When we discussed how soap works, we showed how calcium and magnesium could attach to two soap molecules, making a large molecule that was too big to stay dissolved in the water. This large molecule is what forms soap scum, and it sticks to skin, hair, and fabric, and glues dirt onto them.
To make it easier for soap to work, we remove the calcium and magnesium ions from the water. One way to do this is to add a chemical to the water that combines with the calcium and magnesium and makes it unavailable to combine with the soap. Citric acid will do this. So will sodium carbonate, which is why it is called washing soda.
Another way to remove the calcium and magnesium ions is to use a water softener. A water softener runs the water through a tank full of tiny plastic beads. These beads have a negative charge, and collect positive ions.
At first the beads have sodium ions around them. But sodium ions only have a charge of +1. Calcium and magnesium have charges of +2, so they are attracted to the negatively charged plastic beads more than the sodium ions are. The two types of ions then exchange places. That is why the plastic the beads are made of is called an ion exchange resin.
At some point all of the sodium ions have been exchanged for calcium and magnesium ions, and the plastic beads have to be recharged with sodium ions. To do that, the water softener gives then an overwhelming supply of sodium ions by bathing them in very salty water. Even though the calcium and magnesium ions are more positive, there are so many sodium ions that the reaction goes into reverse.
Once the plastic beads are again coated with sodium ions, the salty water, which now has lots of calcium and magnesium ions in it, is flushed away, and the water softening cycle repeats.