Figure 2. Small bar magnets laid side by side move so that the North pole of one is near the South pole of the other.
Now suppose we place one of the bar magnets on the table. The other, we'll fix on a pivot so it can spin around. This one we'll move so its N pole almost touches the fixed magnet's N pole. As soon as we release it, the movable magnet will spin around so its S pole will be near the N pole of the stationary magnet. That's an electric motor—almost.
Figure 3. A movable bar magnet pivots so its South pole is near the North pole of a stationary magnet.
It's not quite a motor because the rotating magnet will just move as far as it has to in order to get the opposite poles together. You might be able to cause the movable bar magnet to make turn after turn. You could do this by turning the fixed magnet quickly end for end. This wouldn't be very practical as a motor.
We Can Improve It
If we could change the pole on one end of the rotating magnet just as soon as it reaches the attracting pole, it could make a complete circle. In doing that, the pole at the near end of the rotating magnet would be repelled by the stationary magnet and pushed away. As soon as the opposite end of the rotating magnet would come into the magnetic field, it would be drawn to the stationary magnet. In order to keep the "motor" running, we would have to constantly change the poles at each end on every half revolution.
We Need An Electromagnet
We can't reverse the poles on simple bar magnets, but we can on electromagnets. We can make one by wrapping a wire several times around an iron core to form a coil. This magnet will also have a N and a S pole when connected to electrical current. The big difference is that the poles can be changed instantly by reversing the current in the wire.