RE: How do freight trains work....
The way freight trains are operated may be different in different parts of the world. They are certainly different by what the train is handling. My answer is informed by contemporary practices in the U.S.
The simplest type of freight train operation is the "unit train". Technically, this term in the U.S. is used for trains carrying a single commodity from a single shipping point to a single destination. Examples of these types of trains are coal trains used to transport coal from a mine to a power plant, and grain trains, usually complete trains of covered hoppers loaded with any of a number of types of grain, from a terminal elevator, often to a port. Though they are not technically unit trains as such, there are other trains which are similar, in that they originate at one point, and are operated intact to another. Intermodal trains (trains carrying exclusively trailers and/or containters on flat cars) are pretty much unit trains. Except for the occasional need to switch out a bad-order (defective) freight car, unit trains do no intermediate switching; locomotives are coupled to the car at their origin point, and the train is handled as a unit to its final destination.\
The other extreme is local freight operations. These might be in a small geographic area (such as an industrial park in the immediate vicinity of a switching yard), or they might be a local, working a significant portion of the right of way, serving on line industries. In these instances, the train crew is provided with a list of which rail cars go to which industries, and from which industries rail cars are to be collected. Before leaving the starting point, the conductor arranges the cars in the most efficient order for picking them up, and dropping them off; exactly where a car is in the train when it departs the origin point, will depend upon how the car can be handled with the least amount of switching moves. But other rules may come to play, too. In the U.S., there are regulations governing the placement of certain types of loads in the a train. Certain commodities may not be handled adjacent to the locomotive, and may not be handled adjacent to other types of commodities. An example of this is that a carload of a commodity defined as an oxidizer cannot be handled adjacent to a carload of a flammable material. Year ago, on the old Rock Island, a system wide rule required that a jumbo covered hopper car could not be coupled to a caboose, so that even unit grain trains always had another type of car between the last covered hopper and the caboose. Another Rock Island operating rule was that when a train was shoved across a highway grade crossing, if possible, the first car across the grade crossing had to be a caboose.
Most other types of freight trains on the Rock Island were "blocked", meaning that cars going to the same place were located together in the train. There were published books, revised periodically, which specified for each train, in what order the cars were to be placed, and these were revised periodically. The order dictated was the order in which the cars could be handled most effectively. For example, one train left a terminal with a number of groups of cars (blocks); there were blocks for two intermediate terminals, and the balance went to the next major yard on the line. The block immediately behind the locomotive was set out at the first intermediate terminal the train came to; the block immediately ahead of the caboose went to the second intermediate terminal. When the train arrived at the first terminal there was neither yardmaster nor yard crew on duty, so the train set out the block from the head end in the yard on a track which was always left empty to receive the block. If there were cars to be picked up by this train, they were always left on the adjacent track, so they were added to the consist directly behind the locomotive. When the train arrived at the second intermediate terminal, there was a yardmaster and yard crew on duty, and the yard crew tied on to the caboose, and pulled off the block just ahead of the caboose; dropping the cut on the track designated by the yardmaster. If there were cars of to be forwarded on this train, those going to a particular set of destinations were picked up by the yard crew and placed just ahead of the caboose, or depending upon where the final destination of the train, placed into the middle of the train. When the train arrived at the next major terminal, it was in order so that all of the cars behind the the road locomotives to a certain point were going to the same destination. These cars were pulled away from the balance of the train, and cars from the terminal going to the same destination were added by a switch engine which shoved them to the rear of the first block, and the train departed. The next groups of cars were pulled off the train for switching at the intermediate yard, and the cars in the yard with the same destination as the last part, just ahead of the caboose, were attached to the front, road locomotives were attached, and the train left.
ns