NEW DELHI—India’s Supreme Court ruled that gay sex isn’t a crime, providing a victory for millions who have long been exposed to harassment and discrimination in the socially conservative South Asian nation because of a colonial-era law.