An electromagnet works on the principle that an electric current not only allows electrons to flow in a circuit, but also generates a small magnetic field. When a wire carrying electricity is coiled, the magnetic field becomes even stronger. Iron or steel objects surrounded by this coiled electric wire also become magnetized. This combination of electronic energy, coiled wiring and conductive metal object forms the basis of the device.
It may be easier to think of an electromagnet as an electron magnet, not an electric magnet. What is relevant is the free flow of electrons in a circuit and their effects on the wire carrying them. It's possible to demonstrate the basic principles using a supply of bare copper wiring, a D-size chemical battery, and an iron or steel nail.
The reaction between the metals and acid in chemical batteries causes a lot of free electrons to collect near the negative post (-), generally the end with a slight depression. If someone connects the negative end of the battery with the positive post (+), all of those electrons will flow through the wire towards the positive post and eventually make their way back to the negative end. Since there is nothing blocking their path along the wire, such as a light bulb or motor, the electrons will soon stop flowing and the battery will "die."
The flowing electrons do more than run through the wire in a circuit, however. The motion of the electrons causes a slight magnetic field to form around the wire. This field is not especially strong as long as the wire remains straight, but coiling the wire in tight spirals will strengthen the magnetic field many times over as the wire's surface area is condensed.
The coiled wire can generate a measurable magnetic field which can affect a compass reading or small iron filings, but it still needs a means to focus all of the energy. This is where the iron or steel nail comes in. If the wire carrying the electrons is coiled tightly around a metal capable of being magnetized, the metal itself becomes an electromagnet. As long as current continues to flow through the coiled wire from the battery or other source of electricity, the metal core will have all the power and properties of a natural magnet, including positive and negative poles and the ability to attract or repel other magnets.
This ability to alternatively attract and repel other magnetic fields leads directly to the creation of an electric motor. The shaft of an electric motor is nothing more than coiled wires connected to a source of electricity. As the electromagnet alternates between positive and negative polarity, it is either attracted or repelled by permanent magnets surrounding it. This causes the shaft to spin rapidly in one direction and allows the motor to perform work based on that motion.