HMRC works out each person's code based on their 'tax-free Personal Allowance and income you have not paid tax on'

See Full Page