Why do oil and water separate?
The short answer
Water molecules are polar, while oils are mostly nonpolar and avoid water, so the two liquids separate into layers.
How it works
Shaking briefly disperses small oil droplets, but they join again and separate by density. Surfactants such as soap can keep droplets dispersed.
A familiar example
Shaking salad dressing briefly disperses the separate layers before they form again.