For train travel stuff your best first resource is Seat61.com, here's the relevant part for Beijing to Shanghai.
From the info there (How to buy tickets) it seems you cannot book on-line directly, but you can go through a travel agent to have them book the tickets in advance.
The trains do sell out, but you don't need to book months in advance (unless you want to). Passengers are allocated seats and although the train will likely be full it'll not be packed, the amount of people / space will depend which class you're travelling in (there's pictures and more info on the link).
I can't define the best way to buy the tickets, the cheapest would be to get them at Beijing station a week or so in advance. Anything else will involve paying a cut to an agent. However it sounds like you're not there for that long so I'd use one of the agency websites on the page I linked above.
One other option is to contact your hotel / hostel and ask if they can arrange tickets for you and have them ready for when you arrive.
As the site says large stations will have English speaking staff, plus you can get your hotel to write down the details in Chinese for you. In my experience if a place doesn't have English speaking stuff, but has a lot of tourists, there are normal people hanging around willing to help you translate for a small tip (although be careful about being scammed, etc).