Early railroads had only a single track between destinations.
Problems arose when a train was met by another going in the opposite direction or was about to be overtaken by a faster one.
This dilemma was solved with the creation of sidings, short lengths of track built parallel to the main line where one train could pull over while the other went by.
The train had been “sidetracked,” meaning that, for a time at least, it wasn’t going anywhere.
We say someone diverted from a goal has been “sidetracked”.