This happens because water is a polar substance and oil is non-polar. Polar substances are attracted to other polar substances, and non-polar substances are attracted to other non-polar substances. Since oil and water have different types of molecules, they are not attracted to each other and cannot mix. Instead, they separate into layers, with oil floating on top because it is less dense than water.