Oil or hydrocarbon as it is often called - is in the broadest sense organic compounds of hydrogen and carbon. These exist in earth's subsurface in either liquid form – which is called (crude) oil or petroleum.
Petroleum and natural gas are formed from ancient biomass, thus the name "fossil" fuels for fuels based on petroleum or natural gas.
Heating of prehistoric organic material lead to the formation of crude oil and natural gas over a long time via a set of complex biochemical and geological processes of pressure and seclusion from air (anaerob conditions). The organic material usually mixed with mud clay, got buried under strong layers of sediment, thus generating conditions of high pressure and heat.
This caused organic matter first to develop to a substance called kerogen and then with even more heat applied over geological time, becoming liquid or gaseous hydrocarbons.
The subsurface reservoirs possibly blend with water and accumulate in chambers beneath the surface: The hydrocarbon compounds produced in this way were probably concentrated by being dissolved in water and transported through sedimentary rocks; the deposits were then trapped in dome-shaped chambers. However, petroleum can also remain oil shale or oil sands – such as in the Athabasca region of Canada.
Prehistoric zooplankton and algae, plants and animals, from sea or lake bottoms, preserved under anoxic conditions, are the basis of today's crude oil and natural gas. Prehistoric terrestrial plants are mostly the basis for today's coal.
Sources: Dmoz Petrolium Online UArctic