For homosexuality, the systems of sexual attraction in the brain need to tune to the gender somehow, and this is a system which, apparently, isn't 100% successful at aligning gender and sexual attraction. So the answer to "why are some men attracted to men" is the same as "why do men have nipples:" it's because women need to be attracted to men and because women need nipples.
And of course it needs to be said that just because someone's sexual attraction isn't aligned to their gender it doesn't mean that they're inferior. We don't measure the worth of a person by their reproductive success.