This is one of the nice things about GSM (as opposed to CDMA, which Verizon/Sprint use). GSM carriers can't block you from transferring SIM cards from one phone to another.
It's for this reason that people had iPhones on T-Mobile long before the iPhone was officially available on T-Mobile.
Well, they could stop you if they really wanted; they could IMEI sniff. AT&T does it to detect "smartphones" so they can helpfully add 'mandatory services' to your plan without your consent - it'd be easy for T-Mobile to block Apple's IMEI ranges from registering on the network regardless of whether your SIM worked or not.
The FCC should have implemented some actual common carrier regulations/a Carterfone-equivalent for the mobile industry. Instead, they're so mired in regulatory capture we get nothing.
It's for this reason that people had iPhones on T-Mobile long before the iPhone was officially available on T-Mobile.