According to my chemistry textbook, soap works something like this:

First, take some good ol' fat. Next, boil the fat with potassium or sodium hydroxide. The salt formed from this acid-base reaction is soap.

The resulting molecule will look kind've like an individual sperm, the tail being a chain of non-polar hydrocarbons and the head being a carboxylate ion bonded to said sodium or potassium.

When you put these soap molecules on some dirt, the non-polar ends will naturally begin to gather around any non-polar stuff (as non-polar solutions do), that stuff, specifically, is dirt. This formation will cause the soap molecules (also called micelles) to create a sphere with all polar carboxylates making up the surface, and, fortunately, all the dirt and gunk trapped within with the hydrocarbons.

When you put this in a solution (i.e. wash it), the polar surface of each soap-sphere will form a solution within the polar solvent of water, and, therefore, be whisked away.