These days most Chinese people buy the ticket online in advance. There are several apps, but I use 飞猪 (feizhu). The interface is very good and easy to use, though it is available only in Chinese.
If you like an English app, Ctrip let you buy the ticket online. You can pay by your internationally available card including Amex. You can also check the timetable.
Even if you book the ticket online, you must go to the ticket counter to get the paper ticket. Just show your passport and booking information on your smartphone and they give you the ticket.
That said, generally if you don’t care about the exact departure time, there are enough empty seats available at the counter, unless it is the busy season.
I would worry less about the Shanghai-Beijing train being full than the Hong Kong-Shanghai train – unless you travel over Chinese new year or another holiday. There are trains in intervals of 10-40 minutes and should allow you to at least get on the next train in case the current one is full.
There is a website that lets you check the availability of seats on each train in advance, so you can get a picture which times of the day are the most busy. Since the website is blocked here for some reason, go to seat61 and read the section “Do tickets sell out?”. Go to the linked page and do a train search, then click on “More Details”. It will show you how many seats are still free. For example, the 7:00am train for tomorrow morning still seems to have over 500 seats free.
If you do not feel comfortable with this information, I would consider asking directly the question “How full are the Shanghai-Beijing trains on normal Weekdays/Weekends”.
Credit:stackoverflow.com‘