Fungi existed before plants, and definitely before plants evolved to the point of being what we'd consider trees. In fact, we find large fungi fossils that once likely lined the landscape like trees do now.
Lichens pretty much were what created the first terrestrial organic material. Which in turn became soil for mosses which then built up the soil layer more and so on