The Western Railroads were mostly government sponsored. There's an old book by Gabriel Kolko entitled "The Triumph of Conservatism" that discussed this era. "Conservatism" in left-leaning-historical terminology generally means government sponsorship of private enterprise.
Yes, the government owned vast amounts of land in the west that was of zero economic value unless railroads were built. The government gave railroads land for rights of way and land to sell off to settlers. In return railroad built roads that allowed settlement of the continent all the way to the Pacific Ocean prior to the end of the 20th century.
The railroads themselves were built by private companies using iron and steel made in private factories made from raw materials from private mines. A few years ago I was reading Stephen Ambrose's book about the building of the transcontinental railroad. He pointed out that that the US transcontinental railroad was built in less time with a tenth of the Chinese labor that the Russians used to build the trans-Siberian railroad.