So, setting aside any moral or ethical questions, the facts as I understand them are generally down to what are called “shopkeeper’s privilege” or the statutory replacement in your particular state.
This means that Wal-Mart or whomever does have certain rights above and beyond what a normal citizen might have to try and stop shoplifters. But they are fairly specific.
Not showing your receipt is your right. In fact, receipt-checking is, as mentioned, very little to do with shoplifting at all, but that is part of it.
A shopkeeper cannot, in general, keep you on their property. That would be… I don’t know, kidnapping, or false imprisonment or the like. But they have the right, if they have probable cause that you have stolen something, to detain you until the police are summoned, who will then make the decision to arrest you or let you go.
Not showing a receipt is not probably cause. Even setting off the store alarm is not, in general, probable cause. Probably cause would be more on the lines of, a store employee saw you remove the security tag from an item and walk out, or saw you stuff something in your bag and walk past the register.
Granted, exercising all of these rights may be more trouble than it is worth. Obvioiusly the fact that laws exist will not necessarily preempt anyone from doing anything to you, and your only remedy may be protracted litigation. Maybe just showing the damn receipt is your preference. But those, as I understand them, are the general standards.
Disclaimery: I am not by any stretch of the imagination a lawyer. Just an interested layperson with more money than sense. I am probably wrong about everything. Second, if you have signed an agreement with a store like Costco, you may have more obligation to show a receipt. Note that in this case, though, their remedy is limited to revoking your membership. They still cannot detain you without probable cause. Thirdly, your state may vary, and if you are outside the US, I really have even less of a clue. Shopkeeper’s privilege comes from the common law, so any common law countries PROBABLY have something at least similar, but I would have no idea.