When a metal and a semiconductor are brought together, their Fermi levels align, causing electron transfer between the two materials. This transfer results in the formation of a depletion region in the semiconductor and a built-in electric field. The energy difference between the metal's work function and the semiconductor's electron affinity defines the height of the Schottky barrier.