A microphone is a transducer which converts acoustic energy into electrical energy. It is the exact opposite of a loudspeaker, and this is why you can use headphones as a microphone... although since headphones are optimized for a different form of energy conversion, your results may leave a little to be desired.

There are three main types of microphones, based on the way that they convert energy. These are the dynamic moving coil, the dynamic ribbon, and the condenser microphones. Without going into great detail (mainly because I don't know too many specifics), here are basic descriptions of the methods of operation.

A dynamic moving coil microphone consists of a diaphragm coupled with a coil of thin wire, the latter of which is placed in the air gap of a magnet, able to move up and down along with the diaphragm. When sound pressure differentials hit the diaphragm, its surface vibrates along with the sound waves, and in turn causes the coil to vibrate in exactly the same fashion. Because of the way solenoids work, when the coil moves up and down within the magnetic field, analog current which matches the original sound wave is induced in the wire, whose ends lead out to the output of the microphone.

A dynamic ribbon microphone is similar to the moving coil mic; it contains a very thin, corrugated metal ribbon stretched inside the air gap of a magnet, which is tied down at the ends and free to move in the middle. When sound strikes the ribbon, from either side, the ribbon vibrates and interferes with the magnetic field in the gap, inducing a voltage differential in the ends of the ribbon which has a waveform similar to the waveform of the sound wave striking the ribbon. Wires are attached to the ends of the ribbon, and lead to the microphone's output.

A condenser microphone consists of a gold-plated plastic diaphragm, which functions as one plate of a capacitor. The other plate is also gold-plated, and the two are seperated by air. A certain voltage, usually 48V, is applied to the diaphragm, so that when sound strikes the diaphragm and causes it to move, the distance between the plates changes, altering the capacitance of the condenser, and ultimately inducing a varying voltage in the second plate which is an analog of the original sound waves. The signal carried from this plate goes has to go through an amplifier inside the mic before it can be sent out. Thankfully, the amplifiers used for this purpose can be made out of transistors now. Back in the day, tube amplifiers were the only kind available, so condensor microphones had to be rather big.