Railways were late arrivals to cities. So if a railway were to come to a city, room had to be made for it.
In some cases the railway station was built on the edge of the city (although now it might not seem so as houses will have been built around the station).
In big cities (such as London) large numbers of houses had to be knocked down to make room for track and stations.