Unfortunately, there is not yet a conclusive study which tell us exactly what causes homosexuality. Many studies show correlations, but there is not an accepted scientific consensus on the cause of homosexuality.
All credible scientific organizations state that sexual orientation is influenced by biological factors and environmental factors (scientifically speaking, the hormonal environment of the womb is considered an "environmental factor'), and that it cannot be changed, as it is innate and set at birth.
In studies with twins, researchers have found that far more of them are likely to share the same sexuality than with siblings that do not share the same DNA; however, the number falls short of 100%. These results show that there is a high correlation with a person's genetic makeup and their sexuality. Neurologically speaking, gay men tend to have brains similar in structure and function to that of straight women, and lesbians tend to have brains similar to straight men. Certain neurological responses, like the startle response, also show this correlation. The same is also present in other species (yes, many animals exhibit bisexual or even primarily homosexual behavior.) There have been other trends documented, such as the fact that the more older brothers a boy has, the more likely he will identify as gay, and this is true even when the boy is not raised with his older brothers. Gay men are also more likely to be left-handed. The ratio of the length of the index finger to the ring finger, which is caused by hormones in utero and does not change as one grows older, also shows correlations between gay men and straight women, and lesbians and straight men. Some theories include that the hormonal balance of the womb, which influences sex development (whether or not the child is a boy or girl or intersex), influences a child's predisposition to a certain sexual orientation.
If a female and a male are twins, sometimes the testosterone from the male affects the female embryo's development. Females thus affected are more likely to develop lesbian tendencies than other females. Considering the 26th pair of chromosomes in humans, due to particular rare genetic factors, some people born with XX chromosomes become males as opposed to females and some people born with XY chromosomes become females as opposed to male. These people are more likely to exhibit homosexual behaviour.
So "nature" determines one's overall predisposition to a certain orientation, but "nurture" (the environment and experiences of one after birth) may influence other aspects of one's sexual preference, like ideal traits in a partner, fetishes, etc. However, this is a highly complex question, and there is still much more research to be done. Scientific studies on different aspects of this question are being released all the time in journals.
As far as why homosexuality is a healthy trait for a species (and is thus encountered in nearly every animal species on our planet), there are several theories, but to make this point one needs to clarify the difference between a survival behavior and a cultural behavior.
For instance, in current United States culture one of the larger causes of teen suicide is the hatred and rejection shown to homosexuals. This is a cultural behavior. The current United States culture chooses to show disdain and pass judgment on people who have a sexuality outside the cultural norm. This results in some teenage homosexuals committing suicide. Homosexual behavior in a society that has not condemned or sanctioned sexual behavior is considered normal and entertaining. This is still true in some modern countries and tribes, but the culture that most people will be familiar with is that of the ancient Romans and Greeks.
The Greeks believed that men who were in love would fight more fiercely for one another and honored their love in poems and theater. The most famous of these pairings was between Patroclus and Achilles in Homer's Illiad. Their culture believed that love was plural and that a man should love his wife and his friends. By their standards, someone who was only interested in women or only interested in men would be strange (though not despised).
So the scientific explanation may simply be as simple as this: Our bodies have evolved to give us pleasurable feelings when we enact the act of reproduction whether it be to reproduce or not. Therefore, the scientific explanation for homosexual behavior is the same reason for heterosexual behavior or masturbation. . . it feels good. That is not a flippant or intentionally funny answer either. Most human behavior can be reduced to two main goals: avoiding pain and seeking pleasure.
As with all aspects of human nature, the origin of homosexual behavior must stem from evolution. Evolution leads to instinct, which in turn leads to the experiences of pleasure (to encourage us to do things) and pain (to ensure we do not harm ourselves). Although homosexual behavior has been observed among many species of animal, it is primarily (if not exclusively) found among social animals. So it fair to assume that being a social animal allows for homosexuality to exist as others within social groups as others can continue the species' survival. Within social groups there can be diversity, and this diversity can boost a species' survival.
Human sexuality differs also from, say, a dog. A male dog would not be aroused by a female bitch unless she is in 'heat'. Humans do not follow this pattern of behavior as straight men may find women attractive even when they are not ovulating at the time. In fact, only three species on Earth have heterosexual sex outside the 'optimum' period for reproduction: chimpanzees, dolphins and humans; these three species are often regarded as the most 'intelligent' species on the planet. This indicates that some time in our evolutionary past a 'break' occurred between sex and reproduction and this proved, from an evolutionary point of view, highly successful.
So it is possible to see that homosexuality was part of a broader evolutionary past and this led to the richness of diversity of human nature today, of which one of the results was homosexuality.