If you are looking to travel from Bangkok to Koh Samui by train, there are a few options you can take travelling either via Chumphon or via Surat Thani.
All trains heading south stop at either of these main stations on Thailand's Southern Railway Line.
The most popular way to travel to Koh Samui by train from Bangkok is to take an over night sleeper train to Surat Thani Railway Station from Bangkok's new main railway station, Bang Sue Grand (Krung Thep Aphiwat Central Terminal).
There are also many trains that travel throughout the day if you want to take in the sights of the southern Thailand countryside.
The trip takes between 8 and a half and 12 hours depending on the type (and cost) of the service you choose.
From the railway station, there are many bus services (run by the ferry companies) to the various ferry ports where you can take the boat over to Samui Island.
Please note: the Surat Thani Train Station is not actually in the city and there is very little reason to stay around here, it is just a place to connect to buses to the jetties.
If you do get stuck, there is a cheap budget hotel (The Queens Hotel) a couple of minutes walk away from the station if you need somewhere to spend the night.
Another option is to take the shorter train ride from Bangkok to Chumphon Railway Station, from where you can take much less frequent boats to the island of Ko Tao.
These same boats then continue on to Koh Phangan and Koh Samui.
The train trip to Chumphon is a couple of hours shorter at between 6 and a half and 9 and a half hours long, but the boat trip to Samui is longer (and more expensive).
Chumphon however is a much nicer place to hang around for a day or two if you want to, with plenty of nearby beaches to visit outside of the city.