Railroads have to pay to maintain the tracks they run on. They also have to pay property tax for owning the rails and land.
Trucks run on public highways that the government pays for. Trucks do pay taxes, but not nearly as much as they would need to to cover the wear they cause to the roads.
If all freight had to fully fund their own infrastructure, overland cargo would be almost exclusively carried by rail, even more so if they had to cover the damages of their carbon and particulate emissions.