1) ensure iTunes is authorized for your iTunes store account(s)
2) connect your device and right-click or control-click it in the iTunes Source list, then choose transfer purchases from the shortcut menu that appears.
3) sync the device
iTunes will wipe the iPhone but, since you transferred your purchases in the previous step, your content will be in your library and you can re-populate the iPhone with it.
above works for purchases from the iTunes store. for everything else, check out this post by Zevoneer.