The spectrum of "illegal" is broad. One way in which racial epithets are (indirectly) illegal is via anti-discrimination laws, indeed labeled "harassment" by the EEOC
Harassment is unwelcome conduct that is based on race, color,
religion, sex (including sexual orientation, gender identity, or
pregnancy), national origin, older age (beginning at age 40),
disability, or genetic information (including family medical history).
Harassment becomes unlawful where 1) enduring the offensive conduct
becomes a condition of continued employment, or 2) the conduct is
severe or pervasive enough to create a work environment that a
reasonable person would consider intimidating, hostile, or abusive.
Anti-discrimination laws also prohibit harassment against individuals
in retaliation for filing a discrimination charge, testifying, or
participating in any way in an investigation, proceeding, or lawsuit
under these laws; or opposing employment practices that they
reasonably believe discriminate against individuals, in violation of
If an employer tolerates racial epithets, they are likely to be the target of a harassment lawsuit. There is no limit on who utters the epithet, thus a customer can be the trigger for a suit. In cases not involving supervisory employees, liability arises if the employer "knew, or should have known about the harassment and failed to take prompt and appropriate corrective action". Analogously, racial epithets in educational institutions are actionable. Framed in terms of a random epithet by a non-employee or campus visitor (where the institution has minimal leverage over the offender), the institution must act, when it becomes aware of such circumstances arising, and cannot just say "What can we do??".
A second (remote) possibility is through the fighting words doctrine, that the government can limit speech that is "likely to incite immediate violence or retaliation by the recipients of the words". This arose initially in Chaplinsky v. New Hampshire – one of the holdings was that
The Court notices judicially that the appellations "damned racketeer"
and "damned Fascist" are epithets likely to provoke the average person
to retaliation, and thereby cause a breach of the peace.
There have been numerous refinements of the doctrine over the decades, so the mere utterance of a racial epithet would not run afoul of properly-restrained "breach of peace" laws. One of the two most-relevant current opinions is Brandenburg v. Ohio, 395 U.S. 444, where the court held that the government cannot
forbid advocacy of the use of force or of law violation except where
such advocacy is directed to inciting or producing imminent lawless
action and is likely to incite or produce such action
but uttering an epithet is not advocacy of force or law violation. The second is R.A.V. v. City of St. Paul, 505 US 377 where defendant was charged with violating an ordinance that
prohibits the display of a symbol which one knows or has reason to
know "arouses anger, alarm or resentment in others on the basis of
race, color, creed, religion or gender"
SCOTUS ruled that "the First Amendment does not permit a state to use content discrimination to achieve a compelling interest if it is not necessary to achieve that interest" (emphasis added), holding that
A few limited categories of speech, such as obscenity, defamation, and
fighting words, may be regulated because of their constitutionally
proscribable content. However, these categories are not entirely
invisible to the Constitution, and government may not regulate them
based on hostility, or favoritism, towards a nonproscribable message
they contain. Thus the regulation of "fighting words" may not be based
on nonproscribable content. It may, however, be underinclusive,
addressing some offensive instances and leaving other, equally
offensive, ones alone, so long as the selective proscription is not
based on content, or there is no realistic possibility that regulation
of ideas is afoot.
A further holding is that the law is
is facially unconstitutional because it imposes special prohibitions
on those speakers who express views on the disfavored subjects of
"race, color, creed, religion or gender." At the same time, it permits
displays containing abusive invective if they are not addressed to
those topics. Moreover, in its practical operation the ordinance goes
beyond mere content, to actual viewpoint, discrimination.
The court has not clearly identified a context in which a law against a racial epithet would be constitutional, they have simply identified other bars that would have to be cleared for such a law to be permissible. No utterance, devoid of context, is illegal, so to discover where such an utterance could be part of a prosecutions, we would need a lot of specific context in the hypothetical.