There are so many impurities and dirt mixed in water, and most of all the dirt do not dissolve in the water. Soap molecules are a combination of salts such as sodium or potassium. The molecules are of a long chain of carboxylic acids. So, when the carbon chain is dissolved in oil and the ionic end is dissolved in the water, the soap starts cleansing and trapping the dirt. When this happens, the soap molecules form structures that are called micelles are used for capturing the oil droplets and then the other end being the ionic faces. This will then form an emulsion in water and help in dissolving the dirt or impurities when the clothes are washed.
The soap molecules have different properties at different ends. The first end being the hydrophilic end which dissolves in the water and is attracted towards the water and the second one being the hydrophobic end is dissolved in the hydrocarbons and is repulsive to water. The hydrophobic tail aligns itself along the surface of the water because it is not soluble in the water.