Restore iPhone 3GS
To bring your iPhone 3GS back to factory settings you will need the USB connector and your 3GS.
Plug your 3GS into your computer via USB,
Your iPhone should automatically be recognized by iTunes,
When your iPhone is finished syncing your files, you may want to make a backup
To make a backup of your current iPhone state, right click "Your" iPhone on the left side and click backup, this will take a couple of seconds.
After you are finished backing up your 3GS(which is completely optional) you need to be in the "Summary" tab of your iPhone.
Press "restore", by pressing restore, your iPhone will be completely wiped, it will no longer be jailbroken or unlocked, it will download and install the latest update for your 3GS, this will take some time.
If you would like to jailbreak and unlock your device you will need the custom IPSW file for the current version installed on your 3GS, if you are having trouble jailbreaking and unlocking, there are guides that you can find in the "iPhone Hacks" section of MacRumors.
Hope this helped.