This is a frequent question which results in an array of complex answers involving some ifs and whens. Booking your ticket online in advance depends on the train type, time of travel and how flexible you want to be.
Regional train? No.
In general, regional or commuter-type trains like those going from an airport into town, trains connecting smaller towns, or trains within cities, do not need to booked in advance and they have fixed prices. In fact, purchasing your ticket online for these types of trains will issue a ticket that is non-refundable and train-binding. This means, you can only take the one train specified on your ticket, at the time or during the time window printed on your ticket. For those types of trains, we recommend purchasing your ticket at the station.
Long distance trains? Yes and No.
For long-distance trains, it can make a lot of sense to purchase your ticket online in advance. See more information in the section about long-distance trains below.
Do it like the locals!
As an example, Portuguese train travelers hardly pre-purchase their train tickets. Trains usually won’t sell out, but it might be advisable to book your ticket in advance if you are traveling on a national holiday.