First of all you need to be jailbroken, but this is not covered here.
Go into Cydia and click "Manage Sources" then "Edit" and add:
Then search for "ultrasn0w" as a package and install it, that's all there is to it, your iPhone is finally unlocked so when you travel abroad you can avoid ridiculous roaming charges, or you can switch providers and use any SIM card you like.
You'd really think that such an advanced phone should come wide open and really that all cell phones should legally be sold only if they are fully unlocked. If you buy a phone through your provider, you are paying the full cost the hardware whether through a long contract or outright, you should have the right to use the phone on the provider of your choosing.
This is yet another reason why people have to keep jailbreaking the iPhone.