Imagine what would happen if the coil carrying the electric current is allowed to turn freely in a magnetic field. When the coil moves within the magnetic field, it acquires kinetic energy (kinetic energy is the energy that comes from moving objects). This kinetic energy from the turning coil can be used to drive a motor.
An electric motor uses electromagnets to drive the motor. Imagine taking the electromagnet you made with the nail, and flipping the magnetic field over and over again by changing the direction of the current (as you did when you turned the battery around). This would change the positive and negative poles of the nail. If you put this unit inside a horseshoe type magnet (known as the permanent magnet), the north pole of the nail would be attracted to the south pole of the magnet. But what happens when you reverse the poles of the electromagnet? The whole thing would turn around, because like charges repel and opposite charges attract. If you keep reversing the current, the poles keep reversing, and the electromagnet keeps spinning. This is how an electric motor works. In an electric motor, the “nail” is replaced by an armature.
The illustration below is an open source direct current electrical motor (DC Motor).