Allergic reactions come from activity in your immune system. Your immune system looks at every protein it comes across as either "friend" or "foe." Allergies happen when the immune system reacts against a food or environmental protein that should be a harmless "friend." This takes time and usually repeated exposures.
Why you react to an allergen that has not triggered an allergy in the past is one of the mysteries of the immune system. This process is known as "sensitization." In some cases, sensitization happens after your immune system is exposed to a very large "dose" of an allergen. For example, it is common for people who have multiple bee stings, and are exposed to a lot of bee venom, to develop an allergy to bee venom.
In the case of a food allergy, some scientists think that certain illnesses could cause allergens in your digestive tract to become more "visible" to the immune system. This might allow your immune system to spot proteins it had not seen before and start to react against a food protein.
An infection can stir up a reaction from the immune system. Sometimes good antibodies (which fight a bacteria or fungus) can "cross-react" with proteins from another source. Scientists think autoimmune problems -- when your immune system attacks your own tissue -- can be triggered by certain infections. It is possible this is a trigger for allergies as well.
Most allergies begin in childhood, but the reaction does not occur the first time a person is exposed. Allergies can also arise in adulthood.