Bacteria have a lipid encasing that soap will adhere to, soap has two ends, one that likes water and one that likes fats. lipids are a fat.
One end hooks onto the lipid coating, the other end hooks into the water. this allows the bacteria to be washed away.