The contents were cooked or at least pasteurized before being packaged and sealed. Once that seal is broken however, bacteria can get into the product and it will spoil. Refrigeration will inhibit bacterial growth, keeping it good longer.
Because when they are made and packaged, they are made in a sterile environment and have likely had the air removed. This means while still sealed, there is no bacteria or other pathogens in the packaging. When you open it, outside air is introduced which contains millions of microscopic organisms, which then get to work on making more of themselves in the product. Cold slows down (but doesn't stop) this effect. This is why they can last for months or years unopened, but maybe a few weeks after opened, even in a refrigerator. Leave it out in the open air on your counter top and those pathogens can reproduce fast enough to make it spoil in days or even hours.