How Soap Cleans
Soap is an Emulsifier
A soap micelle has a hydrophilic head that is in contact with the water and a center of hydrophobic tails, which can be used to isolate grime.
Soaps are sodium or potassium fatty acids salts, produced from the hydrolysis of fats in a chemical reaction called saponification. Each soap molecule has a long hydrocarbon chain, sometimes called its 'tail', with a carboxylate 'head'. In water, the sodium or potassium ions float free, leaving a negatively-charged head.
Soap is an excellent cleanser because of its ability to act as an emulsifying agent. An emulsifier is capable of dispersing one liquid into another immiscible liquid. This means that while oil (which attracts dirt) doesn't naturally mix with water, soap can suspend oil/dirt in such a way that it can be removed.
The organic part of a natural soap is a negatively-charged, polar molecule. Its hydrophilic (water-loving) carboxylate group (-CO2) interacts with water molecules via ion-dipole interactions and hydrogen bonding. The hydrophobic (water-fearing) part of a soap molecule, its long, nonpolar hydrocarbon chain, does not interact with water molecules. The hydrocarbon chains are attracted to each other by dispersion forces and cluster together, forming structures called micelles. In these micelles, the carboxylate groups form a negatively-charged spherical surface, with the hydrocarbon chains inside the sphere. Because they are negatively charged, soap micelles repel each other and remain dispersed in water.
Grease and oil are nonpolar and insoluble in water. When soap and soiling oils are mixed, the nonpolar hydrocarbon portion of the micelles break up the nonpolar oil molecules. A different type of micelle then forms, with nonpolar soiling molecules in the center. Thus, grease and oil and the 'dirt' attached to them are caught inside the micelle and can be rinsed away.
Although soaps are excellent cleansers, they do have disadvantages. As salts of weak acids, they are converted by mineral acids into free fatty acids:
CH3(CH2)16CO2-Na+ + HCl → CH3(CH2)16CO2H + Na+ + Cl-
These fatty acids are less soluble than the sodium or potassium salts and form a precipitate or soap scum. Because of this, soaps are ineffective in acidic water. Also, soaps form insoluble salts in hard water, such as water containing magnesium, calcium, or iron.
2 CH3(CH2)16CO2-Na+ + Mg2+ → [CH3(CH2)16CO2-]2Mg2+ + 2 Na+
The insoluble salts form bathtub rings, leave films that reduce hair luster, and gray/roughen textiles after repeated washings. Synthetic detergents, however, may be soluble in both acidic and alkaline solutions and don't form insoluble precipitates in hard water. But that is a different story...