Nitrosamines are primarily formed through the interaction of nitrites, which are often used as preservatives in foods, with amines present in proteins. This reaction can occur during cooking, especially at high temperatures, or in the acidic environment of the stomach. Environmental sources, such as industrial processes and agricultural activities, can also lead to nitrosamine formation.