What does the following sentence mean and what is its origin?
Ich verstehe nur Bahnhof.
I've heard this expression from some friends – I guess it's what you say when you don't really understand what's going on.
You're right with your assumption about the meaning.
It means that someone can not follow or does not understand a conversation or what it is about.
Another common subject on which this can be used is the plot of a movie or maybe a play or similar. In this case the meaning is explained above, but can also include, that the plot isn't making sense to the person who's saying it.
"Ich verstehe nur Bahnhof" can also be used if you don't understand what is going on.
According to Wiktionary this figure of speech has its origins in the first World War:
The soldiers apparently were tired of fighting and wanted to return home. Since train was the primary means of transportation, many soldiers associated the train station (Bahnhof) with returning home. Because the soldiers thought a lot about returning home it could happen that they could or would not follow the conversation because they "only understood train station" (which is the literal translation for "nur Bahnhof verstehen").
To add to Lukas anwer (that goes back to the novel "Wolf unter Wölfen" by Hans Fallada) it is worth noting that the origin of this saying may date further back to the 19th century when coachmen in Berlin only understood "Bahnhof" to lead their cab to any of the various railway stations there (this may have then been the farthest).
Unfortunately there is no trusted reference to further proof this view.