Depends on the phone... and the network.
Some networks will do it free under certain conditions, others for a (sometimes nominal )fee.
Some people on Ebay can provide codes for other phones.
Some phones can be done with some software jiggery-pokery yourself.
SIM unlocking, is not the same as rooting though.