One of your replies got some of it but I think it misses some other factors so I'll take a swing:
Costco requires a yearly membership to shop there, which along with the shopping (if you go for the more expensive membership anyway) also gives you yearly cash back on all your purchases, which if you go there for the essentials you can, really does add up quite a bit. My wife and I get basically everything we can from there and usually end up with about $250 of essentially free money per year, and despite being Costco cash, if you go there and don't spend it all, you just get cash in return. Clearly they want you to use it there, but it is required.
Additionally there's an element of curation, though this is more subjective. Walmart will sell anything they can get their hands on, and Costco by comparison has a reputation for selling not exactly the top end bespoke stuff, but at least good stuff. I can recall maybe 2 times in the entire time I've shopped there for the last 6 years or so that I've gotten something I was disappointed in the quality of.
In addition you have the other things like the free samples everywhere, there's much less impulse buying on offer, it just feels... I don't know, you just feel like they actually give a shit? Maybe not a huge shit but a shit and finding businesses that give even a microscopic shit is pretty hard these days. And along with that, they pay their employees extremely well (in the context of retail anyway).
And also like, it's a bit classist for sure, but I do enjoy that there are no "people of Walmart" there, for the same reason I go to Target instead of Walmart for whatever I can't get at Costco. It seems at times Target's entire business model is knowing people who can will pay about 11% more for everything so they can not go to Walmart, I certainly do and will continue to.