Trucking is massively tax-payer subsidized. Railroads own the land their rails run on, pay taxes on that land, and pay 100% of maintenance cost of the rail infrastructure (tracks, bridges, etc). Trucks pay a gas tax... which goes toward that massively subsidized interstate highway system and network of state and local highways. That gas tax does not cover the entire cost of the infrastructure that they use. If tolls were high enough to actually cover the cost of highway construction and maintenance, or if railroads were given the subsidies that highways were given, then rail would be more economical than trucking.
Rail is inherently more efficient and has less environmental impact. Think of a train of 50 cars. Each car can have 2 intermodal trailers on it. That's 100 containers being transported from one location to another using a crew of two engineers. Transporting 100 containers by truck would need 100 trucks and 100 drivers. A truck at max weight (40 tons) gets ~5mpg. That means that 100 trucks would use 20000 gallons of fuel to go 400 miles. A train would use 1 gallon/ton, or 4000 gallons to move that same load 400 miles.
A 4 lane highway occupies a minimum of 80 feet of ROW (right of way). A single rail line requires only 17ft of ROW. Double track would require 30ft. The environment footprint of rail is way less than highways. Then think of the highway interchanges... those sprawling multi-acre parks of concrete. A rail interchange requires a lot less land.
Rail really is better, especially for transporting large volumes of goods. You may say "Yay, but all these places that ship/receive goods are all spread out" ... well, they weren't always. Until the 60s, rail was the primary shipping method, and if you had a plant, factory, or warehouse, you were next to a rail-line. The separation of those businesses from rail physically happened in conjunction with the cost of truck transport becoming so much less than rail as the interstate highway system was built.