The molecule of soap constitutes sodium or potassium salts of long chain carboxylic acids. In the case of soaps the carbon chain dissolves in oil (hydrophobic) and the ionic end dissolves in water (hydrophilic)
Dirt and grease is usually hydrophobic. The soap molecules thus form structures called micelles .In micelles one end is towards the oil droplet and the other end which is the ionic faces outside. Therefore, it forms emulsion in water and helps in dissolving the dirt when we wash our clothes.