The answer for Allah is simple and straight forward: "it is a test"
The answer for evolution is more complex.
It can be any of or a combination of the following answers (and there can possibly be more):
It is possible that homosexuals actually do reproduce more because of their homosexuality. Homosexuality is seldom exclusive. So, homosexuals in nature could still have sex with the opposite sex and maybe their homosexuality would make them more successful in finding a reproduction mate or would make them more successful in raising their offspring.
It is possible that homosexuals help their close relatives reproduce. This would help the genes that promote homosexuality. An example is ants: Worker ants are sterile, yet they make sure the ant Queen can reproduce. Maybe, genes that promote homosexuality help their own reproduction by creating family members that care for their siblings, nieces and nephews.
It is possible that genes that promote homosexuality are beneficial because they also enhance the chance of survival of some of their carriers (Genes often have more than one effect)
It is possible that genes that promote homosexuality are beneficial because they make some of their carriers more fertile (Genes often have more than one effect). E.g. it could be possible that one of the genes that promote homosexuality in males promotes fertility in females.
It is possible that genes promote homosexuality in some environment but promote something else in another environment.
It is possible that genes that promote homosexuality lead to homosexuality in the presence of some genes and beneficial in the presence of some other genes.
It is possible that genes that promote homosexuality are close to other genes that are beneficial and are therefore most often passed on together with those beneficial genes.
...