It’s mostly software locks and the coding on the SIM card, but certain models are exclusive. For instance, while one can get an unlocked Motorola Moto-series, you cannot get an unlocked Motorola Droid-series as those are exclusive to Verizon. Likewise, when iPhones first came out, only AT&T carried them.
But there are hardware reasons as well. Verizon and Sprint use CDMA for their US cellphones while the other carriers (including non-US Verizon/Sprint) use GSM. CDMA phones don’t use SIM cards while GSM ones do. Well, aside from the fact that some Verizon phones are capable of using both GSM and CDMA; if they couldn’t, they would be totally useless outside of the US. However, unlocking the GSM part requires a SIM card that enables international roaming, and getting one of those SIM cards generally warrants your carrier billing you for the privilege. Anyways, the fact that Verizon uses CDMA is also part of why the early iPhones were unavailable for Verizon; they literally could not connect to Verizon’s network.
Different carriers also have some different bands and… well, it gets complicated. I think that reading this will help. It may not clear things up much, but it will at least give you an idea.
The best bet is to buy one that is unlocked. It’s usually pretty easy to tell as they are marketed as such; it’s a strong enough selling point on the same level as how much storage it has or how large the screen is.