Understanding the operation of the PN junction, is the basis for an understanding of all semiconductor devices. The junction is actually manufactured as a single piece of material, but it is much easier to explain the operation, if we imagine producing two separate pieces of N type and P type material and then "sticking" them together.
The free electrons in the N type material and the valence electrons in the P type material, are in random motion due to thermal energy. This alone would tend to distribute the electrons and holes evenly throughout their respective materials and this is assisted by the repulsion between like charges. When the N and P type semiconductor are brought together, the random motion on its own would have a tendency to redistribute the electrons and holes throughout the combined material. However the resulting displacement of charge, limits this to a thin region around the junction.
The overall motion described above results in the net migration of electrons from the N type semiconductor across the junction. These electrons recombine with the holes in the P type semiconductor. This has two important effects:
A positive charge builds up on the N side of the junction, due to the loss of electrons. Similarly a negative charge builds up on the P side, due to the accumulation of electrons. This creates a barrier voltage across the junction, which prevents any further migration of electrons. ( For Silicon this is about 0.6 to 0.7 volts, for Germanium it is about 0.2 to 0.3 volts.)
The number of free electrons close to the junction in the N type material, is depleted due to the migration of the electrons across the junction. The number of holes on the P side is also depleted, due to the recombination of electrons and holes. This depletion layer is about 1 micrometre wide, ( 1 millionth of a meter!) and extends across both sides of the junction. The loss of majority charge carriers within this region, significantly reduces the conduction properties within the depletion layer.