It works because soap is made up of molecules with two very different ends. One end of soap molecules love water - they are hydrophilic. The other end of soap molecues hate water - they are hydrophobic. Hydrophobic ends of soap molecule all attach to the oil.