It can be illegal to dumpster dive behind stores in those stores private dumpsters because the dumpsters behind stores are private dumpsters on private property.
So if they have signs saying no dumpster diving then you can get into trouble and be ticketed or arrested if you continue to dumpster dive in the private dumpsters.
Most stores however will not care if you do dumpster dive as long as you do not leave a mess behind but some stores frown upon it and will call the police because they don't want the liability of you getting hurt or sick from the food or stuff that you take from the dumpster.
If there are signs saying stay out and no dumpster diving or they warn you to stay away from the dumpsters then do so.
If the trash can or trash is out on public property such as at a park, or set out by the curb then it's not considered stealing to take things from the garbage or trash.
Trash cans and trash that is on private property can be considered stealing so if there are dumpsters or trash cans on private property then you cannot enter the private property to go through the garbage.
But if the dumpster or trash can or trash is out on the curb or on public property then going through the trash and taking things set out for trash is not illegal.
The trash or garbage that has been put out on the curb for pickup by the garbage men is public property.
So if you put something out by the curb for garbage then anyone who comes by can actually take that said item or items.
When you put something out by the curb as garbage it is now no longer in your possession and you've given up the ownership rights to whatever it is you've set out by the curb.
Once something is put out for garbage it's public property and in the public domain and anyone can come and get it.
So if you don't want to give up the item then don't put it out by the curb.
Also any garbage in garbage cans that are set out on the curb for trash pickup becomes available legally for anyone to dig through.
So whether you like it or not people can legally dig in your garbage cans once they are out by the curb.