Fungi obtain their energy from plants (more rarely from animals or even other mushrooms) though biodegradation of energy rich materials. When talking about classic mushrooms, the fungi generally fall into one of two categories: those that live in symbiosis with trees and are given nutrients for services such as storing water, and those that directly attack living plants or degrade residual plant matter at the forest floor.
Most commonly, they rely on the energy that is stored in wood. Most of the mushrooms you can buy in a store are grown this way, either on wood chips or other plant material such as coffee grinds.