Praise be to Allah, and may peace and blessings be upon the Messenger of Allah.
The basic principle is that it is not permissible for a person to change their actual sex from male to female or from female to male, because this constitutes changing the creation of Allah. Furthermore, merely feeling internally that one belongs to the opposite sex does not make it permissible to alter one's outward appearance or take hormones and medications for this purpose. Rather, the person should be content with the creation of Allah and seek treatment for whatever they are experiencing through permissible means.
However, it is necessary to distinguish between changing one's sex and correcting the sex and revealing its true nature.
If a person is born male or female, but their genital organs are not properly developed or contain a defect that conceals their actual sex, it is permissible to perform a surgical procedure intended to reveal the person's true sex and correct their organs. Likewise, it is permissible to use whatever treatment or hormones are necessary to strengthen the characteristics of the sex with which Allah created the person. This is not considered changing the sex; rather, it is treatment and correction of a congenital abnormality.
As for someone who is born with both male and female genital characteristics, a condition known as ambiguous intersex (khuntha mushkil), it is not permissible to hastily eliminate one sex and reveal the other. Instead, the person should be examined by specialized physicians and experts, and their condition should be carefully established. Signs indicating one sex may become clearer over time. Once the actual sex has been established, it is permissible to perform surgery to reveal it and correct the organs.
Accordingly, the criterion is: if the procedure is intended to change the established sex with which Allah created the person, it is prohibited. But if it is intended to determine the person's true sex or correct organs affected by a defect, it is permissible to the extent of the need.
Therefore, one should not undergo such procedures merely because of personal desire or psychological feelings. Rather, there must be an accurate medical diagnosis by qualified specialists establishing the existence of a genuine disorder in sex determination. Treatment should then be aimed at correcting the defect and revealing the true nature of the person's creation, not at creating a new sex.
And Allah knows best.