This tutorial can be used to jailbreak before applying the UltraSn0w unlock. You can find instructions on how to apply the unlock here. It will also hacktivate your device for those not with an official carrier.
Create a folder on your desktop called Pwnage
Download RedSn0w from here or here and place it in the Pwnage folder. Likewise, download the latest 4.0 firmware from below and place it in the same folder. If you would like to update to 4.0.1 you will need this firmware as well.
4.0.0 (3G): iPhone1,2_4.0_8A293_Restore.ipsw
4.0.1 (3G): iPhone1,2_4.0.1_8A306_Restore.ipsw
Extract the redsn0w executable from the RedSn0w zip file into the same folder.
Important: If you wish to remain at 4.0 then skip Step Two.
Connect your iPhone to the computer and launch iTunes.
Select your iPhone from the list of devices on the left. Now hold down Shift and click the Restore button. Restore is preferred as it won't create any wasted space on your iPhone.
Navigate to the Pwnage folder on your desktop and select the 4.0.1 firmware ipsw. Click the Choose button to continue.
Once iTunes has finished updating your iPhone to the desired firmware open the Pwnage folder on your desktop and launch the redsn0w application from the redsn0w folder we extracted earlier.
Once RedSn0w opens click the Browse button
Select the 4.0 firmware ipsw we placed in the Pwnage folder on our desktop then click Open.
Important: Select the 4.0 firmware ipsw even if you updated to 4.0.1.
Once the firmware has been verified click the Next button to continue.
RedSn0w will now prepare the jailbreak data
From this window you can select the jailbreak options you would like.
Verbose boot displays detailed information on boot of your device and is generally only needed by developers and advanced users.
Make sure Cydia is selected. For this tutorial we will also select Custom boot logo and Custom recovery logo.
When you click to select each custom logo option a dialog window will appear asking you to select the image. Locate the image you would like to use then press the Open button.
*Remember the rules for boot logos are: RGB or Grayscale format with Alpha channel and dimension below 320x480. If you would like to use the iClarified ones they can be found here: Boot Logo, Recovery Logo
Notice that RedSn0w has already selected to Enable multitasking, Enable homescreen wallpaper, and Enable battery percentage. If you do not need multitasking we would recommend deselecting it as it makes your iPhone 3G run very slow.
Click the Next button to continue
Please plug your iPhone into the computer and make sure its OFF then click the Next button
RedSn0w will now guide you through the steps to get into DFU mode. You can find more help with DFU mode here
Hold down both the Home button and the Power button for 10 seconds.
Release the Power button and continue holding the Home button until RedSn0w detects the device.
Your iPhone will now reboot
RedSn0w will then begin uploading the new RAM Disk and Kernel.
Once this is complete you will be notified that RedSn0w is done. Click the Finish button. When your iPhone finishes rebooting (5 minutes or so) it will be jailbroken with Cydia on the SpringBoard.
You can now unlock your device using UltraSn0w by following these instructions.
*As usual a big thanks to the iPhone Dev-Team for their hard work and contribution to the iPhone community.