A couple of experiences:
On a Canton-Hong Kong train, at the border between the PRC proper and the SAR (Chinese bureauspeak for colony), it was a case of 'everybody off' to file through the immigration hall for passport check...
On crossing the BE-NL, at a time of heightened security, the border guard actually came out of his hut and got on the train to take a (very quick) look (only a momentary glance) at the passengers
On visiting La Cure (Suisse) I wandered along the street - into La Cure (France), passing the border post - from which the (french) border policeman came out to check my passport: when I asked for it to be stamped, he took it in, and took long enough to have called Paris to confirm he could do so, before returning it.
First time I was there, emerging from the station at Nova Gorica (then YU), to be faced with a fence (high steel one) separating the station from the town/forecourt square (in Gorizia (IT)): this was not a border you could cross - you had to go along the road between the border and the railway, to where there was a cross road that crossed both, and you could choose which town to visit (crossing the border or not). Later, Nova Gorica was in Slovenia), the fence was gone, and you could enjoy the square (Piazza della Transalpina or Trg Evrope) - but still not cross the border there: you had to leave the square into the country whence you had come. Now, of course, its EU/Schengen and you just wander at will... When built, the station was Görz Staatsbf, of the kkStB, on the Wocheinerbahn (later Transalpina (as part of FS), and now Bohinjska proga (JŽ/SŽ)
First time I went to Poland, we had a ride on a corridor train through the USSR: didn't stop, and weren't allowed off while passing through. We had an escort of Soviet border guards - an officer (dressed up to the nines, who came through to look at passports, but didn't have any English to practice), and 5 well wrapped up men - 4 to stand in the open doors at the corners of the coaches, to check no one jumped off, the fifth was a spare in case one fell off...
Coming back from Poland by train across the DDR - at the frontier satation (Frankfurt/O?) the smartly dressed Volksgrenzschuzter quickly jumped on the train, and then had to wait for his Polish opposite number (not so smartly dressed) to come and do his stuff - we all had to be stamped out of Poland, before we could be stamped into the DDR. And then - needing a transit visa, and having had one on the way out, knew and had the right money ready - only for it to be rejected as too old (it was 5p and 10p pieces, before they were changed to the current small ones - mine were George VI one and two shilling pieces - not in his book of acceptable foreign currency - I had to delve. They both had small chest born desklets, that could opened up) for their stamps, etc. And then there was the crossing of the DDR at Berlin Friedrichstrasse Fbhf (looking out at the steel wall, that separated it from the S-bahn parts of the Station). And the following day (that I had in West Berlin) riding the (Western) U or S-bahn lines, and interchanging at Friedrichstrasse between two of them, found the station had a duty free (only one on a Metro?) - the DDR wasn't going pass on the chance to get hold of some DM...