In p-type materials holes are majority carriers and electrons are majority carriers in n-type materials. When the two types of semiconductor materials are joined together, the electrons from the n-type material diffuse into p-type material and combines with holes as their concentration is higher in n-type layer. This creates a layer of negative ions near the junction in p-type material. Negative ions are formed because the trivalent impurities (e.g., Aluminum) now has an extra electron from the n-type material. Similarly, the holes from the p-type material diffuse into n-type material resulting in a layer of positive ions in the n-type material.
These negative ions creates an electric field in the direction from n-type to p-type. As more electrons diffuse into p-type material, the electric field strength goes on increasing. The electrons from n-type material now diffusing into p-type material will have to overcome the electric field due to negative ions. At one point, the electric field becomes sufficiently strong to stop further diffusion of electrons.