The Short Answer
If you can only pick one and this is your first trip to China, the honest answer depends on what you actually want. Beijing has the heavier historic weight — the Great Wall, the Forbidden City, the Temple of Heaven, the Summer Palace — sites that define most people's mental image of China and that you genuinely cannot see anywhere else. Shanghai is the more modern, walkable, visually striking city day-to-day, with a skyline, a colonial-era riverfront, and a food and nightlife scene that feels more immediately accessible to a first-time visitor, but without the same density of must-see historic monuments.
If you have 7-10 days or more, do both — they're genuinely different experiences of China and most travelers who do both come away saying each city surprised them in a different way. If you only have 4-5 days total, pick based on the trade-off below rather than trying to split limited time between both and seeing neither properly.