Coals is formed from the remains of ferns and trees. If they die in marshy wetlands they do not decompose this can be due to lack of oxygen of acidic conditions which prevents bacteria form carrying out decomposition.
Over time they are compressed by sediment forming coal.
Oil
Formed by plankton
When they die they settle in mud on the sea-bed
If no oxygen is present they don’t decompose
Overtime they are compressed by sediment forming crude oil.
Natural Gas
Mainly methane.
Formed by plankton similar to oil.
The carbon in was part of the taken in by photosynthesis.