Foam occurs when pockets of gas are trapped in a solid or liquid. We see it all around us: from the foam in the waves on the beach to the foam in the beer we're about to drink, from the mattress we use for camping to the sponge we use to wash up.
The incorporation of gas occurs mainly by mechanical action, that is because we violently beat the liquid in the presence of gas. However, there are substances that, by lowering the surface tension of the liquid, can facilitate this mechanism: our beloved surfactants.
So, surfactants help form foam AND surfactants clean. No wonder that the cleaning power of a detergent has always been perceived as related to its foaming power!
In reality the connection is not so direct. I won't go into detail about the forces at play at the molecular level [1,2]. Let's just say that, while the ability to create foam has to do with how the surfactant molecules interact with the molecules on the surface of the liquid, the cleaning power is determined by how the surfactant molecules interact with the dirt molecules.