A generator makes use of the wonderful connections between electricity and magnetism, two seemingly separate phenomena that become interrelated in the presence of motion. At its heart, a generator consists simply of a magnet that moves relative to a coil of wire.
In the absence of motion, these two objects, magnet and coil, have no effect on one another. The magnet's magnetic field simply doesn't push on the stationary electric charges in the wire coil. But once the magnet begins moving relative to the coil of wire, charges in the wire begin to experience a force. Usually, the coil of wire is stationary while the magnetic moves. In that case, the moving magnet begins to exert electric forces on the mobile charges in the wire. These electric forces are associated with an electric field. While a stationary magnet has only a magnetic field, a moving magnet also has an electric field. Electric fields push on charges and this electric field does physical work on any charges that move with it through the wires. This work transfers energy to those charges and electric power is generated.
Because energy is conserved, the generator's electric power must draw energy from somewhere. The energy comes from the moving magnet. As current flows through the wire coil, it turns that coil into an electromagnet and this electromagnet does negative work on the moving magnet. The moving magnet loses energy as the electric current carries that energy away. Overall, no energy is created or destroyed.
In a modern commercial generator, the moving magnet is typically a huge electromagnet. It spins in the midst of giant wire coils and pushes electric currents through those coils. A steam turbine keeps the magnet moving and heat from a fire or a nuclear reactor provides the high-pressure steam needed to keep the turbine spinning.
Answered by Lou A. Bloomfield of the University of Virginia.