Some have sex change surgery. These categories aren't rigid. Someone who identifies as heterosexual may have same sex fantasies. A gay man may have been married to a woman in the past. A woman who thinks of herself as straight may become sexually involved with another woman.
People discover their sexual orientation in different ways.
Some are sure from the time they are very young. Some spend years trying to figure things out. Some feel certain for a long time, then have a period of doubt or change. But there is no test that says for certain someone is gay. Find friends who can give support if you run into trouble.