Railroads are any armies logistic backbone in WW1 and WW2, so going where the railroad goes with firepower, protection and enough coal and water to travel at 30mph for hundreds of miles without resupply is very useful. Basically the point of an armored train was, when guerrillas blow up the railroad tracks you roll up the armored train as a mobile fort to protect repair workers. Guerrillas in Russia and Yugoslavia, and some other parts of the world like China, were not always small bands. Often that had hundreds of men who could attack the railway tracks, and it took heavy firepower from armored trains to drive them back. The lack of roads often precluded rapid movement of tanks; some armored trains could unload tanks and infantry to attack overland as well. The guns on a armored train were usually much heavier then the guns of typical tanks prior to 1942 and the train had large stocks of ammo and supplies so even if disabled by damaged track or a bombing it could hold out until relieved. This sort of mission was relevant even in the prewar era, the Germans had armored trains in the 1920s and 1930s for example in ordered to use them to stop armed strikers and protesters from shutting down rail traffic. Armored trains could exploit industrial rail lines to go into factory areas and down public streets and a lot of other places you don't see so many railway lines anymore in the west, so the ability to disperse strikes ect... was considerable.
Air attacks were always a threat and destroyed many armored trains in the war, but the better built Soviet and German armored trains did have anti aircraft guns protection and the armor protected them against strafing and near misses. It took heavy bombs falling on the train to destroy it, and that was easier said then done. Even if damaged the components could often be salvaged.
While mainly for use in the rear areas the Soviets in particular used them whenever possible for combat against conventional forces. In the flat open Russian plains an armored train with 76-107mm artillery pieces might be able to shoot at Germans ten kilometers away in any direction. That’s a very potent area of influence when you have 200 rounds per gun to shoot off and can then withdrawal rapidly down the tracks to get more. The very smoothness of that constricted railroad track meant the armored train was a good gunnery platform!
I know of three books on armored trains which I have and are full of pictures. Really good books, I bought all of them and no regrets if you like trains and armored vehicles. The books are German Armored Trains in World War II Volumes I and II, by Wolfgang Sawodny, and Armored Trains of The Soviet Union by Wukfrued Kopenhagen.