There is a "pancake style" electric motor/power generator that goes between the gas engine and transmission. The "free-spinning" of the motor (via the engine) makes power and charges batteries. *basically* -- there is more to it, but that's the core of it.
Yeah, so when you're coasting (in gear), the extra energy goes to the battery. The energy from applying the brakes charges the battery as well. Like taikahn said, there's more to it, but that's the gist.