Brief explanation of what happens when you upgrade or restore:
The space on your iDevice is partitioned into a small partition for the operating system, and a large one for user data and apps. When you jailbreak, part of the OS is "stashed". Many Cydia apps and tweaks need to be stored together with the operating system, and to make space for that, half the operating system is moved to the user partition when jailbreaking. This has the possibility of causing trouble when upgrading.
When a new iOS is released, you can upgrade (and unjailbreak) in several ways.
Over-the-air upgrade reportedly does not work, as the upgrade process notices that something is strange, and aborts.
Using iTunes to
upgrade will usually work fine (unless you have some strange tweaks applied). But it will leave the old "stashed" stuff behind. You will have 500MB of inaccessible junk from the old version in a directory, and if you used Winterboard all the themes will be there too. Mostly harmless, but takes up some space. Some tweaks may survive this process, if it worked by modifying a setup file. Then you are left with no way to turn it off. You can optionally rejailbreak (if a jailbreak is available for the new version!) and clean up manually. This was what I did when going from 5.0 to 5.0.1 - and I strongly recommend choosing the next option instead
Using iTunes to
restore will back up all user data, wipe the iDevice, install the new version of iOS and restore all user data. In principle, some tweaks may survive if they work by modifying a configuration file and that file was backed up and restored. But if you reset such stuff before beginning the process then this is perfectly safe. This it what I recommend. I did it to clean up when I had messed up my phone.
The last option is using iTunes to restore and set up as new phone. All traces of a jailbreak are of course removed. You can then sync your calendar and apps, but this will be significantly more work than the previous option, and only recommended if you want to be 100% sure that noone can detect the jailbreak (like before letting Apple repair it).
And do remember that iOS 5.1 is not yet jailbreakable - and that and iPhone 4S iPad 2 cannot be downgraded once upgraded, and other devices only if you prepare it
before the upgrade.