A p-n junction is the interface at which p-type silicon and n-type silicon make contact with each other. A diode is a p-n junction, with nothing more than p-type on one side and n-type on the other. A transistor has at least two p-n junctions.
To understand how a p-n junction works in a diode, begin by imagining two separate bits of semiconductor, one n-type, the other p-type. Bring them together and join them to make one piece of semiconductor which is doped differently for either side of the junction. Free electrons on the n-side and free holes (think of a hole as an “anti-electron”) on the p-side can initially wander across the junction. When a free electron meets a free hole it can drop into it. So far as charge movements are concerned this means the hole and electron cancel each other and vanish. As a result, the free electrons near the junction tend to eat each other, producing a region depleted of any moving charges. This creates what is called the “depletion zone.”
Now, any free charge that wanders into the depletion zone finds itself in a region with no other free charges. Locally it sees a lot of positive charges (the donor atoms) on the n-type side and a lot of negative charges (the acceptor atoms) on the p-type side. These exert a force on the free charge that can, depending on the polarity of the charge, drive it back to its “own side” of the junction away from the depletion zone.
The acceptor and donor atoms are “nailed down” in the solid and cannot move around. However, the negative charge of the acceptor’s extra electron and the positive charge of the donor’s extra proton (exposed by it’s missing electron) tend to keep the depletion zone swept clean of free charges once the zone has formed. A free charge now requires some extra energy to overcome the forces from the donor/acceptor atoms to be able to cross the zone. The junction therefore acts like a barrier, blocking any charge flow (current) across the barrier. The end result is a junction that is polarized, meaning it is relatively easy to get current to flow in one direction, and relatively hard to get it to flow in the other.