When a material is heated at one end, the atoms at the 'hot' end have more kinetic energy than the atoms at the 'cold' end.
For an n-type material, free electrons are able to diffuse from the hot side to the cold side, thereby generating a potential difference (voltage) across the material which is proportional to the temperature difference across the device.

A similar thing happens to an p-type material, however the signs of the charge carriers and hence voltages are opposite.
The charge carriers in both materials, in effect, carry heat with them from the hot end to the cold end, whilst setting up a voltage at the same time.
If you were to connect an external circuit across the n-type material, heat will be conducted away from the hot junction by the wire itself, so no electricity will flow.
To create a thermoelectric generator, what is needed is to connect both the n-type and p-type material 'back-to-back' with the hot junction, so their voltages are added together, whilst the wires to the external circuit are both connected to terminals at the cold junction, thereby allowing electricity to flow through the external circuit. As long as there is a temperature gradient across the device, there will be a voltage given out across the terminals at the cold junction. Of course, if an external circuit is connected and an electric current flows, heat will need to be added to the hot junction to maintain the flow of electric current.

In practice, there may be hundreds of such junctions connected (electrically in series, thermally in parallel) to generate a 'useable' quantity of electricity.
