The state’s strict law runs afoul of the First Amendment, the high court said in a near-unanimous ruling.