Each train has a manifest which lists the freight cars, their weight, any haz-mat, etc. The railroad's master computer keeps track of where each car is, and where it's supposed to go. In theory. So, a customer can call up to ask about the tank car of molasses which was supposed to be delivered yesterday, and a clerk will tell him "It's at Selkirk yard, and should be delivered to you tomorrow"
Sometimes cars are in dedicated service, so they shuttle back and forth between two points. Like the Tropicana Juice train. Sometimes a shipper will hold onto cars to assure they have cars when their goods are ready to go. Or cars are in special service hauling sulphur, so they wait to be loaded.
Coal plants will often get 100+ car trains of coal, and dump them over 5 days. At the end of the 5 days, the railroad will deliver new loads, and retrieve the empties