You'll first need to jailbreak the iPhone and then unlock it, using one of the various jailbraking and unlocking tools currently available. Search the jailbraking forum here for a full guide to jailbreaking and unlocking your iPhone. Before inserting your T-Mobile card, just be sure to disable 3G in the settings menu and it should be fine after that. 3G does not currently work with the T-Mobile network.