If a person making hiring decisions says that the next candidate for such-and-such role should be a person from such-and-such group (a woman, a person of color, etc.), then the criterion of being the better candidate doesn't apply. Or, rather, it gets adjusted such that belonging in the desired group is included in the list of what makes a candidate better.
I completely agree with you, which is what makes all these recent trends in hiring all the more fascinating to me. If the only concern of a company were the bottom line (which I would expect it should be), then there wouldn't have been any conversation about marginalised groups etc., because, for companies, a candidate's demographics would have been irrelevant. The only thing that would have mattered is which candidate is expected to be the best fit for a given position. That it doesn't work like that suggests that companies are also political creatures, and are concerned about their public image and, possibly, about things like ESG scores. It may even be that the public image and things like ESG scores are a strong contributor to the bottom line.
In reality, people often hire the one they like best - which often tends to naturally be someone who resembles themselves.
Not to mention, very very few companies and positions within those companies operate at a level where they will succeed or fail based on whether they have the best possible candidate. For the vast majority of jobs, a candidate has to be adequate, and you can easily find 10 adequate candidates for every position you want to fill.
It's just like hiring the best street sweeper in the world is not going to significantly impact your bottom line - most CRUD jobs aren't too different from that.
If you're working on cutting edge technology, then finding ten adequate candidates is actually pretty tricky, because by definition the applicant pool is very small. Doubly so for fields that have high demand compared to the supply.
But the reality is that the vast majority of tech jobs are in the first category. There are far more people building web sites than kernel network stack hackers.