Surfactant molecules constitute the part of the detergent mixture that does the actual cleaning work. In most laundry detergents the main surfactant used is an anionic surfactant. These surfactants 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 negatively charged heads 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 since it was this 'polar' end which was keeping them floating aloft in the aqueous solution before.
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 may also be added to the detergent to help to prevent mineral precipitation of the surfactant from occurring.