Coal was formed around 300 million years ago when the earth was covered with swampy forests.
When plants in these forests, mainly trees, mosses, ferns, and reeds died, they fell into the swamps.
Thus, there was a thick layer of dead vegetation in the swamp formed.
With passage of time, more and more plants died and got accumulated. Many such layers were formed, one above the another, thus pushing each layer more inside the earth surface where the topmost layers compressed the old layers.
Heat and pressure caused some physical and chemical changes and eventually, some carbon-rich deposits were formed.
This slow process of conversion of dead vegetation into coal is called carbonization.