Hard water contains salts of calcium and magnesium, principally as bicarbonates, chlorides, and sulphates. When soap is added to hard water, calcium and magnesium ions of hard water react with soap forming insoluble curdy white precipitates of calcium and magnesium salts of fatty acids.
2C17H35COONa+MgCl2 → (C17H35COO)2Mg+2NaCl
2C17H35COONa+CaCl2 → (C17H35COO)2Ca+2NaCl
These precipitates stick to the fabric being washed and hence, interfere with the cleaning ability of the soap. Therefore, a lot of soap is wasted if the water is hard.