Some cell phones are locked to a particular carrier. This is often the case when you purchase directly from a carrier or one of their affiliates. In order to use a 'locked' phone on another network, it must be 'unlocked.' This can be as simple as calling your carrier, sometimes special software is required, other times it must be sent off for a company/service to handle, and rarely it's actually impossible.
Many carriers have a policy of only offering an unlock code (or service) once your contract has expired, although as noted below, there are frequently exceptions. If your carrier won't cooperate at all, there are third-party services that handle unlocking, but you'll have to pay a fee of some sort (generally $40 USD or so). Searching eBay for your phone model and "unlock" as a search term should yield many choices.
Often AT&T phones can be unlocked simply from a phone call. If you call their international support line: (916)-843-4685 and tell them that you'll be traveling overseas and will need to use a foreign SIM card. They'll often just give you the unlock code over the phone, or send it via a text message. Then when you insert a non-AT&T SIM into your phone, it will often prompt for the code. Enter it and voila...unlocked phone.
As with AT&T SIM-based phones, if T-Mobile will not unlock your phone, there are third-party unlocking options available.