Coal is formed by ancient plant materials while oil and gas forms from mostly ancient algae and other micro-organisms found in ancient seas.
Coal forms on land in massive peat bogs that eventually get buried by the sea and other sediments are laid on top of them – after millions of years the peat turns into coal
Oil and gas are derived from algae and other organisms living and dying in the oceans. They eventually get covered by other sediments and the whole lot turns to rock. But the oils and the gases from the dead algae eventually get squeezed up out of their original rock formation migrate upwards in the sedimentary rock basin and then get trapped in a reservoir. See pics.