As for unlocking, most phones (here in the US, anyway) are "locked" to a particular network. In the iPhone's case, that's AT&T for the US, O2 for Great Britain, and so on. Should you want to use your iPhone with another GSM carrier, you need to unlock.
Unlocking lets you place calls with any GSM carrier by inserting different Subscriber Identity Modules (SIMs) into the phone. Normally these SIMs from unapproved carriers won't work. When your iPhone is unlocked, just buy a local GSM-compatible SIM, place it in your phone, and make your calls. You'll be able to use your iPhone around the globe without paying exorbitant roaming fees.
A handset can be unlocked by entering a special code, or in some cases, over-the-air by the carrier. Usually the unlock process is permanent. One example where it is not is the Apple iPhone, which is officially unlocked (when applicable) every time during its activation step. The code required to remove all SIM locks from a phone is referred to as the master code, network code key, multilock code.
Typically, a locked phone will display a message if a restricted SIM is used, requesting the unlock code. For example, on the Sony Ericsson T610 mobile phone, "Insert correct SIM card" will appear on the phone's display if the wrong SIM is used. Other phones may display a "Enter special code" or "Enter unlocking code". Once a valid unlocking code is entered, the phone will display "Network unlocked". In some cases, the phone will simply display a message explaining that it is locked.
The unlock code is verified by the phone itself, and this code is calculated by the network provider, typically by a complex mathematical algorithm that involves the IMEI of the phone in question. The algorithms used in earlier Nokia brand phones (based on IMEI and MCC code) have been reverse-engineered, stolen or leaked, resulting in many people offering Nokia unlock codes for free or for a fee.
Many other manufacturers have taken a more cautious approach, and embed a random number in the handset's firmware that is only retained by the network on whose behalf the lock was applied. Such phones can often still be unlocked, but need to be connected to special unlocking boxes, such as UFS or JAF and the Universal Box that will rewrite that part of its firmware where the lock status is kept, and often even recover a phone that is "bricked" or completely damaged in the software sense. Common characteristics of such phones include no response from the phone on attempting to switch it on, though in some cases it can be recovered or "unbricked" merely by holding some keys down while switching on the phone.
Most phones have security measures built into their software that prevent users from entering the unlock code too many times. After that the phone becomes "hard-locked" and a special unlocking box (mentioned above) has to be used in order to unlock it.
However, certain phones, notably the high-end PDA's and PocketPC's, are unlocked by the use of special programs that require the phone to be connected to the computer and read the unlock code. So, such phones can be unlocked free of cost.
Handset manufacturers have economic incentives both to strengthen SIM lock security (which placates network providers and enables exclusivity deals), and also to weaken it (broadening a handset's appeal to customers who are not interested in the service provider that offers it). Also, making it too difficult to unlock a handset might make it less appealing to network service providers who have a legal obligation to provide unlock codes for certain handsets or in certain countries.
The main reason to unlock a phone is to be able to use it with a different SIM card. For example, when traveling abroad it's usually cheaper to temporarily use a foreign network, for example with a prepaid subscription. An unlocked phone can't access extra cell phone towers or give free phone service. All it can do is accept other SIMs.
In some cases, a SIM locked handset is sold at a substantially lower price than an unlocked one, because the service provider expects income through its service. Consumers may choose to unlock their phone and continue using their previous provider. Therefore, SIM locks are usually employed on cheaper (pay-as-you-go) handsets, while discounts on more expensive handsets require a subscription that provides guaranteed income.
Also, the unlocked phones have a far higher market value, even more if they are debranded. Debranding often also involves special unlocking boxes that remove the operator logo on startup and a variety of limitations that have been imposed on the device by the operator to increase income, as in the case of the iPhone in which the mobile operator AT&T disabled a feature called tethering that allows the phone to be used as a modem on the computer; in other cases, it can be achieved with a home PC, free legal software avaiable online and the USB cable specific to the model.