There are three types of fossil fuels: Oil, coal and gas.
Let's look into their formation one by one:
Oil: Dead plankton such as microorganisms and plants was buried under the sea million years ago, it accumulate over time to become oil, also known as petroleum,
Coal: Plants were dead and broken down by bacteria over time, carbon dioxide and methane were produced. After million years, the dead flora accumulated to become peat and undergone compaction, becoming coal.
Gas: This is formed along oil, and again produced by dead organisms. It is mostly formed during decomposition, and mostly made up of methane.
Hope it helps!