Sri Lankan train tickets cannot be bought online at this time. Reserved tickets (where you are assigned a seat) can be bought in advance of the day of travel at the railway station. Be aware that during peak season, reserved tickets often sell out weeks early. Unreserved tickets can only be bought on the day of travel. If you're taking a particularly busy route (such as Colombo-Galle or Ella-Kandy), always aim for reserved seating. If they're sold out, arrive at least an hour early before you plan to leave in order to get your tickets from the ticket counter.
Note: unreserved tickets hardly ever sell out. This can result in crowded trains with more people standing in the aisles than are sitting. But hey, at least you're always likely to get on board!
If you are able to, always aim for 1st class reserved seats in what is known as an Observation Saloon Carriage. Tickets are a little more pricey in comparison to other classes (Colombo-Galle 1st class is 800 Rs per person, about $6.60 CAD) but you'll have much more space, plus fans in the ceilings.