It uses electromagnetism and a bit of electronic.
You may have noticed a set of strange metal dots arranged below the strings of an electric guitar. Those are magnets and they form the pickups of your guitar.
The oscillation of the metal string above them induces a tiny current that can be "picked up" and sent through wires to an amplifier that boosts this small electricity and, through a loudspeaker, converts it into sound that you can hear.
It is interesting to notice that at the start (in the guitar) and at the end (in the loudspeaker) magnets are used to mechanically convert movement into electricity and vice versa.