An electromagnet is the basis of an electric motor. You can understand how things work in the motor by imagining the following scenario. Say that you created a simple electromagnet by wrapping 100 loops of wire around a nail and connecting it to a battery. The nail would become a magnet and have a north and south pole while the battery is connected.
Now say that you take your nail electromagnet, run an axle through the middle of it and suspend it in the middle of a horseshoe magnet as shown in the figure below. If you were to attach a battery to the electromagnet so that the north end of the nail appeared as shown, the basic law of magnetism tells you what would happen: The north end of the electromagnet would be repelled from the north end of the horseshoe magnet and attracted to the south end of the horseshoe magnet. The south end of the electromagnet would be repelled in a similar way. The nail would move about half a turn and then stop in the position shown.
You can see that this half-turn of motion is simply due to the way magnets naturally attract and repel one another. The key to an electric motor is to then go one step further so that, at the moment that this half-turn of motion completes, the field of the electromagnet flips. The flip causes the electromagnet to complete another half-turn of motion. You flip the magnetic field just by changing the direction of the electrons flowing in the wire (you do that by flipping the battery over). If the field of the electromagnet were flipped at precisely the right moment at the end of each half-turn of motion, the electric motor would spin freely.