Legal gender refers to a person's gender as it is recognized in legal contexts in their country of residence. This may include how their gender is recorded on identification, such as passports and driving licenses. A person's legal gender is normally initially recorded as their gender assigned at birth.
For transgender people, legally changing gender is often a part of transitioning. It may be necessary to legally change one's gender before being able to change name or title. It is also necessary to acquire an ID which reflects current gender identity, which is normally needed to gain employment or housing without revealing transgender status (while "stealth").
Many countries have pre-requisites before a legal change of gender can be recognised. This may include having medically transitioned, or having been diagnosed with Gender Identity Disorder. This is a form of gatekeeping.
In countries which do not have marriage equality for same-gender couples, change of gender can result in any existing marriage being annulled. In the UK, spouses have the opportunity to veto any change of legal gender of their partner.