There are some genetic markers that have been identified (no one gay gene) generally but scientists definitely ascribe environmental factors play a role as well. It's not cut and dry.
I think this seems to be the best understanding we have currently but there does seem to be some evidence that the hormonal environment in the womb during certain stages of pregnancy may be an important factor. Abnormally low levels of testosterone and its correlates in the mother during pregnancy (yes women do have testosterone too, just much lower levels of it than men) does seem to be predict less robust expression of a wide range of male characteristics in boys after birth. This may also feed into the observation that birth order has a bearing on homosexuality in males since testosterone levels decline with age in women as well as in men. We also know that older women are somewhat less likely to carry male offspring to term than younger women. Certain infections and diseases in women during pregnancy can also evidently disrupt the hormonal environment for the foetus and male foetesus appear to be more developmentally at risk from such episodes.
It seems at least plausible that there can be some genetic predispostion to homosexuality, even though evidently not linked to a single gene, an influence of the in-utero environment and finally an influence of the developmental environment after birth, but it appears that no one of these is likely to be decisive in and of itself. Perhaps the 'perfect storm' of two or three of these factors is what determines the final result.
We also have to consider that homosexuality, like intelligence, does not seem to be a single phenomenon but may have several or even many different forms and causes. It is possible that some forms of homosexuality are a behavioral adaptation to having a low expression of masculine physical characteristics, but clearly there are plenty of males with normal or even hyper-masculine physiognomy that also exhibit significant same sex preference, though this group may find it easier to experiment with opposite sex encounters as well. These are just the two most obviously different 'kinds' of homosexuality that spring to mind with the main unifying factor being a simple preference for same sex sexual encounters.
We shouldn't underestimate the importance of habit forming early behavior either. It is normal for young males to experiment sexually with both sexes and, for at least some of those who experience signifiicant early success with one or the other, that can easily become a self reinforcing preference. We tend to want to repeat behaviors that we have enjoyed success with previously.
It seems likely that the larger societal and cultural environment also plays an important role. Historically there have been a number of well known examples of societies that imposed significant attempts to regulate and control the sexual behavior of at least the non-elite class of their citizens. The late Victorian period in Europe and North America following on from the Industrial Revolution is probably the most familiar example of this and the concept of homosexuality as a defining part of personal identiy seems to be something that emerged in the 20th century in reaction to the sexual regulation that was imposed on urban middle class populations during the Victorian era. By their strong prejudice in favor of family formation, Victorian society seems to have created a social and legal box in which to put sexual outlaws that were not conforming to this expectation. Being kept in this box led to the development of a new sense of sexual identity for many of these 'outlaws'.
Before this time, although male homosexual behaviour was and remains common in all societies that we have knowlege of, it was generally not treated as defining personal identity. Rather it was just considered a behavior that was more or less appropriate depending on the circumstances in which it occurred, like swearing or fighting being accepted as normal among young men but not among or in the presence of women or the elderly. Even today there are huge parts of the world where this is still pretty much the case - cultures in which there is a high tolerance or even an expectation of homosexual behavior in certain contexts without it being seen as identity defining. People who are 'politically' homosexual in the sense of self identifying as homosexual seems to be a phenonmenon of the very affluent societies of the post industrial world and a very recent thing in human history - ultimately something not understandable without considering the history of such societies.