This data erasing advice and method also applies to the iPad and iPod Touch
If your old iPhone is a 3GS or an above model, then securely erasing your personal data is simple enough. The 3GS and above iPhone models comes with built in hardware encryption by default (not that you can switch it off), namely the iPhone uses AES-256 encryption, which encrypts all data stored on the iPhone to a strong industry accepted standard. This is not to say your personal data is safe if your iPhone is lost or stolen, due to the way Apple have implemented this encryption, however that is the subject of another blog post, the important thing here is all the your personal data that is stored on the iPhone, is strongly encrypted, therefore by merely deleting the encryption key securely from the iPhone (and everywhere else), will render all the personal data inaccessible.
Built into the iPhone iOS is an option to erase all the data on it and restore it to factory conditions. Apple states the encryption keys are removed (which doesn't take long) and then a series of ones are written to the entire data partition, which is why it takes a couple of hours to complete the process.
"When you opt to “Erase All Content and Settings,” the process can take up to several hours. The time this process takes will vary by device:
Devices that support hardware encryption: Erases user settings and information by removing the encryption key to the data. This process takes just a few minutes.
Devices that overwrite memory: Overwrites user settings and information, writing a series of ones to the data partition. This process can take several hours, depending on the storage capacity of your iPhone or iPod touch. During this time, the device displays the Apple logo and a progress bar." - Apple
The overwrite of the entire data partition with ones post encryption key removal makes the process secure enough to trust in terms of general third party data recovery risk in my personal view, however military organisations and some industries (and the paranoid) may well require further overwrite passes of the data partition with further 1s and 0s, for which there is commercial software available, such as iShredder. If anyone has managed to recover data from an iPhone following Apple's erasing process, I'm yet to hear about it.
How to Erase your Personal Data from the iPhone
1. Backup your iPhone in iTunes, you may well want to restore your personal information to your new iPhone.
2. Make sure the iPhone has power, this process might take a couple of hours to complete, you don't want the iPhone to run out of battery life before finishing.
3. On the iPhone go into "Settings"
4. Then select "General"
5. At the bottom tap "Reset>"
6. Select "Erase All Content and Settings"
6. Tap "Erase iPhone"
7. Wait a couple of hours and you are done.
Finally don't forget to remove the SIM card. The iPhone doesn't store any data on the SIM card but it is a wise precaution just in case your mobile operator doesn't de-active it properly, also its not like the person you are selling the iPhone to needs it anyway.