Apple recently released an update for iOS 4.0 users who were exposed to a pdf file security threat and if you have already upgraded your iPhone 3GS and wish you jailbreak it again then you can use the unofficial release of PwnageTool.
Redmond Pie has published a detailed walkthrough of the entire process which can be easily followed to jailbreak your iPhone. It is important to note that even though the website has posted complete How To steps they do not recommend the unofficial release as it may have bugs.
If you like to regularly update your device and jailbreak it often then you should probably save your SHSH blobs to Cydia which would enable you to downgrade your iPhone/iPod touch to previous version of iOS and jailbreak your device easily using jailbreakme.com
iPhone 3GS jailbreak details are available here.