Unlocking is legal!
If you want the long answer well here it is. In a 2002 unlocking policy review Ofcom clearly states that locked handsets are a restriction to consumers' freedom to choose the operator they choose, including having the option of using two different services simultaneously to take advantage of different offers (for example using one SIM for your phonecalls, another one for browsing the internet, and another one for calling abroad).
Why, you ask, is locking handsets then legal? Well mobile phone companies often have to subsidise the handset in order to make it affordable. When you buy a phone on a contract you are not paying for it upfront, and when you buy it on pay-and-go it is often cheaper to buy it from a mobile operator than it is to buy it from the manufacturer itself. Mobile companies thus subsidise handsets, and Ofcom deems this increases, rather than diminishes, choice in the market. Seeing as they are paying for part of your phone, it only seems to fair for them to try and retain your custom by making sure you stay with them until they have made this money back. So the bottom line is that, as a customer, you can unlock your phone using whichever method you prefer in order to switch between networks as you choose.
There are also several legitimate, established and respectable businesses that provide unlocking solutions to companies and government bodies who want to switch operators after their contract with their existing supplier has ended.
While unlocking itself is legal, the unlocking industry is riddled with grey areas which you may want to know about if you're going way deep, or considering becoming an unlocker yourself. For example, scandals in the industry involve employees leaking unlock codes and selling them on the black market. You've seen how much these codes can cost, so you can imagine how substantially some individuals are profiteering from this practice. Another grey area surrounds the practice of boxbreaking phones, which consists in restoring them to their factory settings by removing all of the software that came with them. In the UK, this practice is legal, but this is not the case around the world.
One practice that is illegal in pretty much every state with a telecommunications law is the practice of unblocking phones. When handsets are stolen or reported missing their MSISDN is registered and then shared among operators in order for them to refuse to connect the phone to their networks. When a phone is unblocked, its unique identifier is changed so that it can be used again. This effectively makes it possible to use stolen phones.
This is not so much an issue for the regulators as it is for the police itself. Why is this relevant? Because the machines (or boxes) that are used to unlock phones can, in many cases, also be used to unblock them. And while most high street unlockers are upstanding members of the local community providing a useful service, there are some that aid and enable thieves and fraudsters to sell phones which shouldn't be on the market.
As an end customer, the fact that you can unlock your phone and that this is perfectly legal remains. You don't need to worry yourself with this though it is good for you to be aware of it in case you're about to unknowingly become involved in an illegal activity. Remember, under UK law ignorance is no defence! But the law is also on your side with regards to this issue. Mobile phone companies are not allowed to make phones unlockable, plain and simple.