Dirt is oily in nature and hence does not dissolve in water.
To removes these dirt we often use soaps that contains sodium or potassium salts of long-chain carboxylic acids.
In soap the ionic-end is hydrophilic and hence dissolves in water.
The end containing carbon chain is hydrophobic and hence dissolves in oil leading to the formation of structures called micelles that dissolves the dirt in water thereby making it easier to clean the clothes.
This leads to the creation of emulsion in water.
But in case of hard water it becomes difficult to form foam and the forms scum with the salts of hard water. In that case another kind of cleansing agent used is known as detergents.