Giles pretty much has it.
Diesel-electric, diesel-hydraulic, and electric locomotives all do run equally well in either direction (and some even have a cab at either end). Even steam locomotives ran pretty well arse-backwards, but the trouble there was visibility, so in the steam days, most major stations and yards, and pretty much all terminals had a turntable. These days, most turntables are gone, and instead there is the cheaper and more space-efficient “passing loop” or a “runaround loop”. This isn’t a loop as you might imagine it though - more like a segment of line where there is a parallel track joined to the main line at either end. Basically, you stop your train on the main line between the switches, uncouple the lcomotives, drive forwards, throw the switch, drive backwards down the other line back over the trailing switch, throw that switch back to straight, then back up and couple to the other end of the train. The last loco is now the lead unit. There is also such a thing as a “balloon loop” which actually is a loop, and in that you simply drive your train around it and wind up facing the other way. Those take up loads of space though and are relatively uncommon.
So, in a “lash-up” of, say, four locomotives, you need the engineer’s cab facing outwards - but only on units 1 and 4. The middle ones can be any which way, and indeed some locomotives are specially designed to operate in the middle, because they have no driving cab at all! These are cheaper to construct, and are called “drones”.
Of course, that is the traditional way of doing it. On longer freight trains, it is increasigly common to use “distributed power”, which simply means putting locomotives in the middle or at the end of the train as well (still controlled by the guy at the front). This is for purposes of increased adhesion and braking power, and if you have other locomotives at the end of the train, you don’t need to do any shunting at all when you want to go home - just walk up to the other end and get in.
Some countries have experimented with a control cab in the rear car of passenger trains that are hauled by locos at one end, and this might be what you are seeing. The British even experimented with doing this in the steam days, with the fireman staying in the loco, and the driver controlling the train remotely.