As other answers have pointed out, for most DB trains, advance booking is recommended but not mandatory.
There exist some international DB trains where reservations are mandatory. In 2023, reservations on the Amsterdam-Oberhausen(-Frankfurt) and Amsterdam-Mönchengladbach lines are mandatory between 17 June and 18 August. Trains to France, Italy, and Poland may also have mandatory reservations. If you turn up just before the train departs on those lines this summer, you will have a high risk that the train is sold out and that you cannot buy a ticket for the train.
For Köln-Frankfurt you should be fine, because even if the train might originate from Amsterdam, mandatory reservations only apply for the part Amsterdam-Oberhausen.
You need to buy the train ticket before entering the train (the option to buy it online is switched off around departure; and only on some trains you are allowed to buy the ticket onboard against an extra fee).
For local trains, there is no particular reason to buy tickets before reaching the train station (except maybe convenience).
For long distance trains, buying tickets at least a few days ahead of time lets you get the "Sparpreis" reduced fare. These advance tickets are bound to a particular connection, so only do this when you know exactly when you’ll be travelling. Depending on demand and how far ahead you book, the Sparpreis can be significantly cheaper (I think I got a Sparpreis for 20ish Euro once, where the full price would have been close to 100), or just a minor discount.
Another advantage of buying ahead of time is that this increases your chance of reserving a seat. A busy connection will probably have no option anymore to reserve a seat a few minutes before departure. You’ll then have to either hope to get one of the unreservable seats, or stand. It is possible, but very rare, that you won’t be allowed on the train at all. You can’t reserve seats on most local trains.
Do I need to book and order a ticket in advance
In Germany, train fares are usually fixed for a specific train (for ICE especially) (they’re called Flexpreis) and will not go either up or down, and you can buy them at the station with no issues.
This is except the discounted Sparpreis and Super Sparpreis tickets. Those are cheaper than Flexpreis, are less flexible (of course), but they sell out and the further in advance you book, the cheaper it will be.
For example :
The Sprinter (faster), service from Frankfurt to Köln at 10:30 on Saturday, 24th is :
On July, 22nd :
Credit:stackoverflow.com‘