Early train from Cologne to Braubach, then visit the Marksburg Castle.
The Marksburg can only be visited with a guided tour. Unless you are planning to take a German language tour, you won't need to take an early train. The first English tour is at 1:00 PM. Even by regional train, the trip from Cologne to Braubach only takes an hour and a half. You could leave Cologne on the 10:32 RE and change in Koblenz (12 min) to the next VIA train up the right bank, and get to Braubach at 12:05. That's enough time to climb the hill and get on the 1:00 PM English tour.
The tour take 50 minutes, and the next train to St. Goarshausen leaves Braubach at 2:06 PM. I don't think you can get down the hill and to the station in 16 minutes, so you will probably take the 3:06 PM train and get to St. Goarshausen KD dock, next to the ferry by 3:40 PM. If the ferry is running on schedule, you just make the 3:40 ferry and get to St. Goar at 3:45. The last KD boat to Bacharach leaves St. Goar at 5:10 PM, giving you 1H25 to see St. Goar. Considering the time to get up to Rheinfels castle and back down and buy tickets at the KD kiosk, I don't think you would have much time at all to see the castle, which, IMO, is just about the only thing in St. Goar worth seeing. Plus, the 5:10 PM KD boat to Bacharach get you there at 6:30 PM. The next train after that leaving Bacharach is at around 7:00 PM, which doesn't give you time to see Bacharach. After that, there is only a night train connection leaving at 9:00 PM.
Bottom line, you either need to leave Cologne earlier and take a non-English language tour of the Marksburg, or you need to spend a night on the Rhein, preferably in St. Goar (or maybe Braubach, Boppard, or St. Goarshausen).
Plus, we haven't addressed what you plan on doing with you luggage all this time.