I've heard many theories for this. It's a known fact that religions (the Abrahamic ones mostly) condemn homosexuality.
The main theory I've heard was that early primitive societies overemphazized the importance of traditional gender dynamics/relationships in order to survive the harsh times. That meant men could only marry one woman (polygamy has been common in most of history). Men were not allowed to be feminine and homosexuals were abhorred. This one actually seems to make sense as it seems to be a trend today. The wealthier countries in the world are less homophobic because traditional gender dynamics are less important for survival.
The other theory I've heard is that it was done to simply stop rampant STD's spreading. It's undeniable that bi/gay man have higher STD rates so perhaps ancient people noticed this and made homosexual sex a sin.