The fundamental observed fact that underlies this is that all unaccelerated observers get the same result when measuring the vacuum speed of light in any direction.
So if an observer, A, riding at the middle of a long train sees signals emitted from the front and back of the train at the same time, since the distances and speeds are equal he or she infers that they were emitted at the same time.
If A raises a flag if and only if he or she receives the signals at the same time, then an observer B on the bank also knows that the signals reached A at the same time. But when raising the flag, A has moved ahead in B’s frame from where he or she was when the signals were sent. So B sees that the signal from the back travelled further than that from the front and so must have taken longer (since B sees exactly the same speed of light in all directions as A). So from B’s point of view, the signals which A saw as being emitted simultaneously were actually not simultaneous (with the one from the back having been sent earlier).