To upgrade your iPhone or iPod Touch to Apple's iOS 4 firmware, make sure you're running the latest version of iTunes on your computer. Here's a link to iTunes 9.2 from CNET's Download.com. After installation, you may need to restart your computer.
Once iTunes is open, and your device is connected, select your device in the iTunes sidebar so that the summary page (shown here) is visible. Take notice of the button for Check for Update but don't click it quite yet.
Just to be safe, make a backup of your settings prior to upgrading the firmware. To do this, right-click on the device icon in the iTunes sidebar and select Back Up. It should only take a minute to create a backup, and it's time well-spent. If anything goes awry during your iOS update, you can use this same menu to select the Restore from Backup option.
Finally, click the Check for Update button from the device summary page. If your iPhone or iPod Touch is compatible with iOS 4, iTunes will tell you that an update is available for download and will guide you through the installation.