When you buy from T-Mobile it will be locked, but, after a certain period of time (not sure how long, 30 days, 4 months?), you can ask them to unlock it. I know if you want to switch to another carrier and have been with T-Mobile at least 30 days, they will unlock it for you to switch, with the EIP program. Also, if you want to use it in other countries, I'm pretty sure they will unlock it, but, still think it's after 30 days.
If you get the phone from Apple, it should be unlocked, but, I would ask them to verify that.
They all (all carriers) have to unlock the phone, if it is paid for, or your contract is fulfilled. After 2 years w/iPhone 5 & AT&T, I asked them to unlock it, as I was getting ready to switch to T-Mobile. No problem, they unlocked it, next time I synced with iTunes, it was unlocked. I didn't use it with T-Mo, though, got the iPhone 6 through T-Mo store. However, when I dropped my 6 in the water, I put my SIM in the iPhone 5 while the 6 sat for 3 days to be sure it dried out (it was fine).