Both AT&T and T-Mobile are GSM providers, like the kind I mentioned.
If you are 1 year through a 2 year contract, you will have to pay an early termination fee. More than likely, you can keep the phone. But, if you want to use the phone with another provider, you will have to get it unlocked through the provider before you terminate your account, say with AT&T, to go to T-Mobile. You can usually pay to have it unlocked, but doing it through the provider is free.
Usually, if the service sucks enough, and you still have a year left on a contract, it makes sense to just pay the ETF, and move on, maybe selling the phone to help with the costs.