Sigmund Freud probably had the right theories.
In some cases it could be genetic, such as transgenders, but I think it's mostly a combination of circumstances (formative years especially) and preferences.
If you look at the ancient world, particularly ancient Greece and Rome, it was quite widespread and part of the culture, so it's more than just a question of genetics.