Or, more simply, one end of a soap or detergent molecule likes water. The other end doesn't.
The end that doesn't will attach itself to anything except water, including the dirt on your hands, or the dirt on your clothes in the washing machine.
Agitation is necessary to increase the efficiency of this process!
The other end attaches itself to a water molecule, and the whole lot, the water molecule, and the soap or detergent molecule carrying the dirt, goes down the plughole.
That leaves soap or detergent molecules hanging onto your hands or clothes. To remove them, you rinse, and the water-loving ends of the molecules are attracted to all that clean fresh water. Again, agitation helps the process, and aids in breaking the bond to your hands or clothes.