How does the food industry know the expiration date?
Just to clarify:

"Expiration dates" (or sometimes "Best if used by" dates) are the dates when a product may no longer be of high quality.  It is not a safety indicator.  It is a quality indicator, and it is just a guideline.  

Companies determine expiration dates during storage studies or stability tests.  More detail can be found here.

The reason why manufactured food items often last longer than home-prepared items is that companies add ingredients to prolong shelf life.  This is simply for economic reasons.  You could add the same ingredients, but it is often not necessary because of the scale of home cooking.