There are of course many different types of fossils, but most form through some sort of mineralization process. What’s that? In water that has lots of dissolved minerals, after an organism dies, those minerals can slowly deposit in the tiniest of spaces within that organism, even within cell walls. Well-preserved fossils require the organism to be covered with sediment quickly after death (like on the bottom of a lake) so that the body doesn’t decay before the slower mineralization process can take place.
This fossil of an extinct trilobyte was formed millions of years ago when the animal was covered by sediments at the bottom of an ocean or lake. Over time, minerals replaced the decaying flesh and solidified into the shape of the original animal.