For Walmart it's obviously a cost, probably higher that the cost of goods stolen.
For the private security: they are literally risking their lives and they are probably not paid a lot. Shop owners get beaten up and sometimes killed when defending their stores.
For the society: we don't want privatized violence.
What is the security guard supposed to do when some big dude (or worse: a group) starts stealing stuff?
Get in a fight? What if they hurt the thieves? It's an assault and they could end up in jail for that.
And in SF the police doesn't bother to arrest thieves because D.A. was refusing to charge them.
So what would even be the point of e.g. detaining the thieves?