A soap molecule has two parts. One loves water (hydrophilic – hydro-water, philic-loving) and the other hates water (hydrophobic – phobic-hating/staying away)
The hydrophobic part sticks to oil and dust and whatever is dirty. It kind of forms a sphere around the particle, with water-loving end outside. This collection of all suck spheres forms the foam. Remove the foam and thing is clean.