How does an electric (AC) motor work?
In the above diagram, you can see two magnets in the motor: The armature (or rotor) is an electromagnet, while the field magnet is a permanent magnet (the field magnet could be an electromagnet as well, but in most small motors it isn't in order to save power).
This is a huge question and I don't know how much detail you want or how much you've had the education to understand, so I'll be general. First a few basic concepts: Current flowing through a wire causes a magnetic field to be generated. Magnetic fields influence each other to move. Alternating current reverses polarity with each cycle. One simple type of electric motor consists of two fixed magnets and a coil of wire wrapped around a central shaft. The coil of wire becomes an electromagnet when an electric charge is applied to it. This then generates a magnetic field. The coil and the shaft it is attached to begin to rotate due to interactions with the two fixed magnets. This rotating shaft is the basis of the electric motor. There are many more details which you could learn if you want to do more research. There are many more complicated motor designs. An electric generator is the opposite of what was just described--physical rotation causes an output of electric energy. If you want a more detailed understanding of the fundamental forces involved, I would suggest taking an electromagnetic field physics class. Or if you just want to learn more about motors get one and tear it apart! Reverse engineering is the best way to truly understand how something works.