Some stacking occurs naturally in any gel electrophoresis, but in SDS-PAGE we take additional measures to promote stacking. When you pour a gel for SDS-PAGE, you will first pour a layer of separating gel. This layer of gel has a high percentage of polyacrylamide (usually 10–15%), creating a dense ”jungle” that the proteins must pass through. The density causes big proteins to slow down, since they cannot pass through the jungle as easily. The separating gel also has a pH of 8.8, and is buffered by Tris-HCl (more on that later).
On top of the separating gel, you will pour the stacking gel. The wells are formed within the stacking gel, which has only 4% acrylamide. The low percentage of polyacrylamide in this gel means that there are lots of wide openings within the gel matrix; very little separation by size occurs here, since most things can get through just fine. The pH in the stacking gel is 6.8, and is buffered by Tris-HCl.
The running buffer used for SDS-PAGE (the one you pour over the gel) is buffered by Tris-glycine, rather than Tris-HCl (which is in both layers of the gel). This means that the running buffer has glycine ions, while the gel has chloride ions.
When the power is turned on, the negatively-charged glycine ions in the running buffer are forced to enter the stacking gel, where the pH is 6.8. In this pH environment, glycine is predominantly neutrally charged. This loss of charge causes the glycine ions to move very slowly in the electric field of the stacking gel. The Cl- ions (from Tris-HCl in the gel), move much more quickly in the electric field. They form a “front” of Cl- ions moving ahead of the glycine. The proteins get trapped between the Cl- ions and glycine ions, and are squeezed into a tighter and tighter band as they move through the stacking gel.
Eventually the whole procession (Cl- ions first, then proteins, then glycine ions) gets through the stacking gel, and hits the separating gel. Here, the pH is 8.8. At this pH, the glycine ions acquire more negative charge, which lets them speed up, accelerating past the proteins. Both Cl- and glycine ions are now moving faster than the proteins, which are now left to migrate on their own. Because the separating gel has smaller pores than the stacking, proteins are sorted by mass.