A sound wave is a series of pressure variations in the air. A dynamic microphone converts these pressure variations into variations in current. The sound wave makes a thin diaphragm vibrate, and the diaphragm is attached to a coil that sits in the magnetic field of a permanent magnet. As the coil moves, it cuts the magnetic field, so a potential difference is induced in the coil by electromagnetic induction. This drives an alternating current in the circuit that follows the pattern of the sound. A louder sound moves the diaphragm further and gives a larger induced current.
A loudspeaker does the reverse. An alternating current from an amplifier flows in a coil that sits in a magnetic field. The current in the field gives a force on the coil, from the motor effect. As the current changes direction, the force changes direction, so the coil and the attached cone move back and forth. The cone pushes on the air and makes pressure variations, which we hear as sound. Headphones work in the same way as a loudspeaker, but on a smaller scale.