There are lots of reasons.
Ideally forces you to think ahead of time what kind of conduct you are OK with or not OK with, so you don't have to decide at the moment when someone you actually have a relationship with does something.
Ideally you think ahead of time how you would respond if someone did one of those things.
Ideally you think about a process for people to follow that balances several contrasting goals: Namely, to avoid doxxing or blaming the victim, but also to prevent the system from being used against innocent people (which it seems what happened here).
Ideally people who are prone to act like jerks decide not to act like jerks at all: Either they behave themselves, or they don't join your community.
If you don't do a Code of Conduct, and just "tell the jerk to stop, and if not take action", then it's a crap shoot whether you overreact (as happened here) or underreact (as has happened in many cases). If you deploy a Code of Conduct properly, there's at least a better chance that you'll respond constructively.
"you repeatedly did x and this is explicitly against our Code of Conduct that you said you'd read before your first contribution"
instead of
"you're being a jerk and as such we will be refusing any contributions from you going forward".
The second example is completely arbitrary, and is up to the whims of the core team - whomever they might be. With a Code of Conduct you're (ideally) told up front what you can and cannot do. This reduces ambiguity and arbitrary decision making.
I can also tell you that a Code of Conduct isn't a change to how I personally govern the projects and communities I'm in charge of. Instead, it's about writing down what I would already remove people for anyway.
It’s not as if a code of conduct ever contains anything that sane people don’t already know.
And like some other commenter said, it basically boils down to “don’t be an asshole”.
Breaking the Code of Conduct isn't a one-and-done thing. You talk to that person, and tell them that what they said was over the line. Be that using a slur, being increasingly insulting to another community member, or something else.
I, as an administrator, basically tell them "not cool". If their reaction is "yeah, alright" then nothing more comes of it. No citations. No three-strike rules. But if someone continually defies our reprimands we have the backing of saying "you've repeatedly been told... " and "this is written in ..." meaning nothing should come as a surprise.
It's also a way for normal users to understand what might be rule-breaking behavior. I can't observe everything happening in a community, as much as I like participating in them. Instead, I have to rely on other non-admin members reporting bad behavior. Making it clear to other users what kind of behavior they can report someone for is also super helpful - and for some people induces trust.
Codifying rules is meant to prevent that from happening, even though as we see it doesn't always work that way.