Anionic surfactactants are very effective at drawing oily materials and oil/clay soil into suspension in the wash water. However minerals which may be present in the water, most frequently in the form of positively charged magnesium (+2) ions and positively charged calcium ions (+2), tend to bind themselves to the electrically charged head of these surfactant molecules. Surfactant molecules which have reacted with the water soluble minerals in this way fall out of solution, as they no longer have the electrically charged head which would have kept them floating aloft.
Depending upon how hard the wash water is, (the harder it is, the more mineral ions are dissolved in it) varying quantities of surfactant molecules are put out of service as they precipitate with these minerals. Different types of anionic surfactant molecules have different tendencies to react in this way with the mineral ions. Tails and heads of surfactant molecules can vary tremendously, hence the great variety and effectiveness of different surfactants. Some surfactants can remain aloft even in harder water, others, such as those consisting of fatty acid chain found in bar soaps, do not fair as well. Because of this, detergents have come to rely on synthetic surfactants which do not precipitate out as easily under high dissolved mineral conditions in the wash water.
Other ingredients which are added to the detergent mix may help to prevent mineral precipitation of the surfactant from occurring.
In an ideal washing situation oils and dirt globules are lifted up from fabric fibers and carried away in a water loving coat of anionic surfactant molecules. Once suspened in this manner, these globules do not re-coalesce onto the fabric strands when water currents bumps them back up against the fabric. This is because the laundry fibers are coated with negatively charged anionic surfactants just as surfaces of the dirt/oil globule are coated. This means that effectively both surfaces are negatively charged and they will repel one another and the dirt globule will never make meaningful contact with the fabric again.
This is what keeps the dirt emulsified in a washing machine. Dirt globules remains aloft for the duration of the laundry procedure and they are expelled in the dirty water at the end of the wash cycle because of this. However, when a lot of mineral ions are present, as in the case of hard water, the positively charged mineral ions can act as bridges - joining the negatively charged fabric-surfactant layer to the negatively charged dirt globules-surfactant layer. To prevent this from happening, the positively charged water hardness ions need to be removed from the system or they need to be rendered inactive in one way or another. Many detergents include a class of chemicals called hardness sequestrants for this purpose.
Hardness sequestrants are chemicals which will prefferentially bind with the water soluble ions, and therefor effectively remove them from the wash-water-action, leaving the surfactant molecules free to carry out their dirt-emulsification processes in peace. Naturally, the laundry detergent industry has devised synthetic hardness sequestrants, and puts these chemicals to use. For many years phosphates were added to detergents to act as hardness sequestrants, these are not synthetics, but every one knows to avoid them for environmental reasons. Washing soda is not a synthetic either, and it's environmental impact is much less than the impact that phosphates have. Yet, it works very well as a hardness sequestrant. Green laundry detergents usually contain washing soda for this reason.