How do thermoelectrics work?

The basic diagrams for power generation and thermoelectric cooling are shown below.
Thermoelectric device power generation mode refrigeration mode
A thermoelectric device fundamentally consists of metal-semiconductor junctions. The above devices can be split into five regions and the one-dimensional band diagram is shown below before they are placed into contact.
thermoelectric band diagram before contact
Once the materials are brought into contact the fermi level must be equal through the device so band bending occurs.
thermoelectric band diagram after joined
How will a temperature gradient affect this diagram? Consider first how metals behave under a temperature gradient. The hot side of the metal has a higher concentration of electrons above the fermi level than the cold side. Diffusion of electrons from the hot side to the cold side occurs because electrons move to where energy is lower; thus removing the concentration gradient. Alternately, the electrons on the hot end have a larger momentum than those on the cold end. Therefore, the hot electrons diffuse faster towards the cold side than the cold electrons diffuse away. (figure adapted from Dr. Foll, at University of Keil, here)
metal seebeck effect
Likewise, the presence of a thermal gradient across the p and n type materials leads to unequal carrier concentration since carriers are thermally activated. The fermi level is tied to the carrier concentration distorting the bands further.
thermoelectric power generation band diagram
The gradient in concentration drives diffusion of electrons and holes from hot to cold. Charges build up when electrons and holes migrate towards the cold side leaving behind charged donors/acceptors. This charge build up leads to an electric field causing backflow of current that will eventually cause the system to reach steady-state equilibrium.
thermoelectric power generation band diagram 2
In the case of thermoelectric cooling devices (Peltier coolers) a potential is applied across the device directing a current through the materials. As an electron moves from the metal to the p-type semiconductor it must release energy in the form of heat to enter the valence band (technically electrons do not transport through the p-type material- only holes, so a temporary electron-hole pair is assumed). This released energy heats the metal. Conversely the electron must absorb energy as it passes back to the central metal region and again as it is promoted into the conduction band of the n-type semiconductor. The heat absorption results in active cooling in this metal region. Finally, the electron leaves the conduction band of the n-type material releasing heat into the last metal region.
thermoelectric cooling device band diagram