When I shampoo my dirty hair, the first time it does not lather very much. The second time it lathers a lot. Why is this?
Quoting a reddit post from chemist nallen:
The short answer is that the dirt and oils from your hair compete for the surfactants making them less available to form lather, which is small bubbles.
To better understand the mode of action, you have to know a bit about the formulation of shampoos and the nature of dirt and oils.
Dirts and oils deposit on hair and fibers because they are at least partially hydrophobic (not soluble in water.) The surface of hair, skin, etc... is more hydrophobic than water (well of course!), so these dirts have greater adhesion for the surface than for water (the water actually pushes them out to minimize surface area.) This is why water is ineffective for removal, it won't pull the dirt off the surface by itself. Surfactants associate with the hydrophobic surfaces to make them more dispersible or soluble in water, allowing them to be rinsed off. So what happens when there is more surfactant than there is hydrophobic dirt surface?
The answer is that the surfactant orients to the surface of the water, because air is hydrophobic, and the water wants to push the hydrophobic portion of the surfactant out of the water molecule matrix. This orientation on the air-water interface is how skin of bubbles is formed. When the water/surfactant solution undergoes sufficient shear mixing, bubbles are formed, and that is lather. Lather typically forms when there is surfactant in excess of the hydrophobic surface. More dirt means less free surfactant, and therefore less lather.
Now, it's not purely that simple, because the amount of lather that a surfactant develops varies with the type of surfactant, some are better than others.
Lather does not mean that the shampoo is cleaning better, it's purely cosmetic. Typical shampoos are primarily SLES/Betaine, that is sodium laureth sulfate and cocamidopropyl betaine. The SLES is there for cleaning, and the betaine is there for lather, it's specifically added to produce lather because, well, people like it. There are some other benefits, but that's the primary one. If you used SLES by itself is would clean just as well, but people just don't like poor lather, so it would not sell.