You can also ask if the hotel you are staying in have the service. Some hotels buy train or airplane tickets for their guests. But train tickets are getting less and less.
If buying at the station, You can buy tickets many days in advance. Be sure to allocate time as the train station ticket booths have very very long lines nowadays, as it is near CNY. This is perhaps one of the worst time to travel in china. Even domestic plane tickets are selling out like hotcakes. I was buying plane tickets online last night and every few minutes I see tickets sell out and/or price going up. Plan and buy early. As far as I know many long distance train tickets are already sold out.
If going to shanghai from hangzhou, remember to ask for Bullet train. In chinese it is called "Dong Che Zu" (动车组

. Ticket should be around 57 RMB each way per pax. I've posted somewhere a while back but cannot find. So I'll repeat the guide for trains in China:
There are 2 train stations. Shanghai station and Shanghai south station. If going north, Shanghai station (上海火车站

has more trains. If going to Hanghzou, zhejiang or south, Shanghai South station (上海南站

has more trains. When taking a cab to Shanghai South station, the taxi driver will ask you if you are taking train or bus, do not panic, there are 2 sections of the station. Tell the cab driver accordingly and he will drop you off at the right location.
The following is the guide to train numbering in China:
DXXX - DongCheZu 动车组 also called D-ZiTou (D字头

, Fastest bullet train. Minimal stops and exceeds 200km/h at max speed. Sells out quickly. Aircon.
TXXX - TeKuaiChe 特快车, minimal stops, quite fast, as fast as 140 km/h at max speed. Aircon.
KXXX - Kuai 快车, despite the name, it is not that fast. Moderate number of stops. Aircon
XXXX - Normal train, many many stops, older train cars, quite slow. Aircon or non-aircon, but non-aircon is getting exceedingly rare.
If you are unable to secure tickets for D trains. try to go for T trains. Thing is, sometimes there are no seating tickets, then ask for bed WoPu(卧扑

, May cost a little more but better than the worst, which is Standing Ticket - ZhanPiao (站票

. Sometimes, in worst situations, when I was travelling during CNY itself, can only get standing ticket. Then I will go to the ticketing booth in the train itself (ask or listen to the broadcast and note the car #) and you can go upgrade your ticket (BuPiao 补票

there if there are seats available. I have taken 5 hours train standing, it is no joke. We sit on floor, behind doors, between cars, and the whole train is packed body to body. No choice. BTW, bullet trains do not allow standing tickets usually. However I have seen a little recently on longer bullet train rides (esp the changsa to shanghai train).
Hope this helps. You might also want to consider taking long distance buses. Although slightly more expensive, it is usually faster than trains (slower than bullet trains though). I have taken the LD buses extensively and it is my preferred method of travelling within Zhejiang province. If you want info on that, I will be another long post. Let me know.