The distance between Shanghai and Beijing is about 750 miles (1200 kilometers). There are two ways to get to Beijing from Shanghai. 1) flight - I don't recommend, or 2) the high speed rail
Flight:
Costs ¥1500-2000 ($220-$290) for a round trip. Best website to book is Ctrip
Flight duration between Shanghai and Beijing is about about 2 hours and 20 minutes
Flights in China are frequently delayed. You have a 10-20% chance of being delayed by 1-3 hours. This is the main reason I don't recommend it
Choose Hongqiao airport (SHA) instead of Pudong airport (PVG). Hongqiao is much closer to the city and is easily accessible by metro. Hongqiao airport also has a high speed rail station attached to it, so if you're going to another city around Shanghai (like Nanjing, Suzhou, Hangzhou, etc.), Hongqiao airport provides the best train connections
High Speed Rail:
Costs ¥1100 ($160) for a round trip. Ctrip is the best website to book. Booking in person at any train station in China is also possible
High speed train takes 4 hours and 30 minutes to get to Beijing from Shanghai. There are also slower high speed trains that take close to 6 hours to get to Beijing, fares are the same. Ctrip will show you the duration when you book so book the faster trains if you can. (The faster trains are only recently rolled out, called Fuxing. I expect most of the trains in the future will be this)
High speed trains almost never get delayed in China. You can count on arriving on the minute
Trains arrive at the Beijing South Railway Station(aka Beijingnan station), which is connected to the city by the metro
By your ticket at least 2-3 weeks in advance (you can buy it up to 30 days in advance). This route is very popular, especially around Chinese New Year in Feb/Mar
You need your passport to book train tickets in China
I highly recommend taking the train. I've done both and can tell you the trains are much more comfortable and less hassle (and cheaper). Here's a video on YouTube of this train (video is not mine):