Like how can you eat meat and fish years after production?
Ordinary food gets spoiled by being exposed to air (oxygen) and by being consumed by microbes (bacteria, mold, fungi, etc).
Canned food is sealed in airtight cans, and then heated to kill all microbes in the can. So there is no air inside the can, and no microbes. Unfortunately this also cooks the food inside, which is why there is no such thing as canned salad greens or canned medium-rare steak.
Bacteria growth is predicated on the fact that there is bacteria present.
If you have 1 atom of bacteria in a canned good, it can multiply over time. That’s what causes dented or swollen cans.
If you have 0 atoms of bacteria in a canned good, 0 multiplies into 0. You can’t have bacteria grow if there is no bacteria.
When goods are canned, they are cooked in a such a way that attempts to eliminate all of the bacteria. This isn’t 100% effective, but that’s the point of a shelf life. The few cans that do still have bacteria don’t have enough bacteria to multiply into a harmful amount by the shelf life. However, after that shelf life, the vast majority of the cans that have no bacteria in them will still be safe to eat.
How long are canned foods actually safe to eat? In the last of us there was a scene where they were eating 20 year old canned raviolis.
In traditional canning it is because the food is cooked within the sealed container. All of the germs die from the heat and new germs have no way of entering.