iTunes Store: Transferring purchases from your iOS device or iPod to a computer - http://support.apple.com/kb/HT1848 - only purchases from iTunes Store
or download purchases directly to your computer too
Downloading (using iOS or computer) past purchases from the App Store, iBookstore, and iTunes Store - http://support.apple.com/kb/ht2519 - enabled with iTunes 10.3 and newer; not all media formats are available in all countries (see: iTunes in the Cloud and iTunes Match Availability by Country - http://support.apple.com/kb/HT5085); apps, books (not audiobooks), music, t.v. shows, and movies (some - not all studios have permitted this). Downloading previously purchased movies and TV shows requires iTunes 10.6 or later. Discontinued items not available. For items not included in the iCloud list (e.g., ringtones, audiobooks), or locations or computer systems where iCloud is not (yet?) available, you only get one download per fee paid. Apple notes it is your responsibility to back up your purchases.
Select the iTunes Store on the left side of iTunes.
Click on Purchased on the right side under Quick Links.
You can re-download your available previous purchases.
A phone can only sync to one library at a time. When you first plug it in it will replace the content with the content of your library. Back up your phone first. Backups are selective (e.g., not music which you should transfer as indicated earlier).
iOS: How to back up - http://support.apple.com/kb/HT1766
iTunes: About iOS backups - http://support.apple.com/kb/HT4946 - including a list of what is backed up.
How to Restore iPhone From Backup - http://ipod.about.com/od/iphonetroubleshooting/ss/restore-iphone.htm