The ruling is a victory for the Asian-American rock band The Slants, which sued after the government rejected its registration to trademark its name.
Band members said they were reclaiming the term, which was viewed as derogatory to Asians.
Band leader Simon Tam said they were "humbled and thrilled" by the ruling.
"This journey has always been much bigger than our band - it's been about the rights of all marginalized communities to determine what's best for ourselves," he wrote on Facebook.
The US Patent and Trademark office had denied the application, pointing to a portion of the law that prohibits federal registration of a trademark if it disparages persons, beliefs, or institutions.
In a unanimous 8-0 decision, the Supreme Court said that clause was unconstitutional.
"It offends a bedrock First Amendment principle: Speech may not be banned on the ground that it expresses ideas that offend," Justice Samuel Alito wrote in the decision.
The US government had said its decision to refuse the band trademark registration was not a violation of free speech because the band could still use the name.
But federal registration is important because it makes trademarks more secure, said Ronald Coleman of the Archer law firm, one of the lawyers who argued the case for the band.
The decision has implications for a case that is already pending over the name of the US football team, the Washington Redskins.
The US Patent and Trademark office cancelled the team's trademark registrations in 2014, after five Native Americans said the name was offensive.
Paul Fucito, a spokesman for the trademark office, said it is reviewing the decision and expects to issue new guidance for employees.
The top US court says the government cannot deny registration of trademarks with offensive terms, arguing that is a violation of the right to free speech.