It wasn't from trees, as far as I can see.
The Intermountain West is not a flat barren desert, but a series of basins divided by small and large mountain ranges. The lower parts of the basins may be arid (like the desert near the Great Salt Lake in Utah), but the slopes and canyons around them are not. Those canyons can be forested.
Since many of those canyons also have running water, water powered sawmills could be and were built to supply lumber for mines, railroads and towns farther down the basin.
Some of the visuals in old Westerns are not completely accurate, in other words, about the physical surroundings of towns. If you want to get a better feel for the landscape of much of the area, a good layman's book is Basin And Range, by John McPhee. It is primarily about geography, but does give a strong description of the area. Well written, too, if you just slip over the technical geographical terms.