Most of the railroads that were built in America were built with the assistance of the Federal government through grants. The railroad companies would receive millions of acres of public lands of Congress so they could build the railroads there and sell the rest gaining money to finance the construction.
Some were not built with the assistance of the government, the smaller ones had to purchase the land from private owners.