Definition
When you run electric current through a tight bundle of coiled wire, a force that pulls on iron appears around it. A magnet made with the power of electricity like this is called an electromagnet. Turn the switch on and it becomes a magnet. Turn it off and the force fades away. Raise the current or wind on more wire, and the force grows stronger by the same amount.
Wrap wire around a nail and paper clips stick
Did you ever wrap wire around an iron nail in science class? The moment you connect both ends of the wire to a battery, paper clips cling to the tip of the nail in a rush. Then you pull one end of the wire off, and the clips drop right away.
When current flows through a wire, an invisible region of force forms around it. This region is called a magnetic field. Iron that enters a magnetic field gets pulled in, just as if it were stuck to a magnet.
The magnetic field from a single strand of wire is very weak. That is why we wind the wire around the nail many times. The weak force from each loop adds up, turn after turn, until it is strong enough to use. A tightly wound bundle of wire like this is called a coil.
Here is the key point: it is a magnet only while electricity is flowing. Take the battery off, and the current stops. The pulling force fades along with it.
How a junkyard crane picks up scrap and drops it
Scale that nail-and-wire experiment way up and you get a junkyard crane. At the end of the crane hangs a big disk made by winding thick wire or metal strips around and around, many, many times. Flip the switch, and a whole car comes swinging up.
There are three ways to make the force stronger. Wind on more wire, send through more current, or put an iron rod inside the coil. The nail in the experiment was doing exactly the job of that iron rod.
The iron rod placed inside a coil is called an iron core. The tiny grains of iron inside the core line up in the same direction along the coil's magnetic field, which makes the field hundreds of times bigger. That is why the clips barely stick if you pull out the nail and leave only the bundle of wire.
When the crane operator wants to set the scrap down, there is no rope to untie. They just switch it off. The instant the current stops, the lined-up grains scramble, and the pile of scrap simply falls.
To be a bit more precise
In the experiment, the clips fall when you take the battery off. But if you hold that nail near a compass, the needle sometimes moves slightly. That is because some of the iron grains that lined up did not fully return to where they started. This weak leftover magnetism is called residual magnetism.
So an electromagnet's force does not drop all the way to zero after you switch it off. Crane cores are made of soft iron, which holds on to magnetism poorly, to reduce this problem. A magnet whose force always stays, like the one on your refrigerator, is called a permanent magnet to tell the two apart.
One more thing: an electromagnet keeps using electricity the whole time it is on. When current flows through a coil, the wire heats up, so large electromagnets need cooling. That is part of why a crane does not hold scrap in the air for long and sets it down quickly.
Common misconceptions
An electromagnet pulls in any metal at all.
What gets pulled in are metals that stick well to magnets, such as iron and nickel. An aluminum can or a copper wire will not stick, even if you place it right under an electromagnet.
The iron rod inside the coil is itself a magnet, so clips stick even without electricity.
The iron rod is not a magnet on its own. It becomes a strong magnet only while current flows through the coil. Once you cut the current, only a very weak magnetism is left.
Where you meet it
Earbuds make sound because a tiny coil attached to a thin membrane becomes an electromagnet. It pushes and pulls against a permanent magnet next to it, which makes the membrane vibrate.
A junkyard crane lifts a pile of scrap with one switch, then drops it just by switching off.
In one sentence
It is a device that acts as a magnet only while electricity flows through wound wire, so one switch turns the pulling force on and off.