To start off, I am only speaking of Judaism. So this answer will differ for other religions.
A good study when it comes to this was done by DS Silber in a paper called The Jewish Dietary Laws and Their Foundation. So this is specifically talking about dietary laws, and not other purity laws, but it gives a nice light into how much of a differentiation there is when it comes to purity laws and how they enter into a religion.
One of the common ideas about Jewish dietary laws is that it revolved around hygiene. This is a view we see in ancient scholars as well such as Moses Maimondes. Maimondes argues that the foods that the Bible forbids all have damaging effects on the body. And the medical evidence supports this to a bit, but it fails after you get past some of the generals. For instance, there is no prohibition on poisonous fruits or herbs. Or if we look at pork, if you just cook it properly, there is little danger. That's not to mention that studies have shown that camel meat other products is just fine. On the other hand, a clean animal, like sheep, can transfer tape worm.
Many are coming to see that the traditional view simply doesn't work. There are other ideas that have risen up to replace it, but probably the strongest proposal is from Jacob Neusner in his article, The Idea of Purity in Ancient Judaism. Neusner's argument centers around symbolism, and really comes down to social association. It separates one group from another.
A good example of this is pigs. Pork was a staple during the Bronze age, and pig bones are commonly found in those sites. In Canaan, the pig was an exceptional food source. They were very well adapted. However, once we begin to more towards Hebrew settlements in Iron Age I sites, very few pig bones are found. Brian Hesse and Paula Wapnish, in Can Pig Bones Be Used for Ethnic Diagnosis in the Ancient Near East?, argue that eating pork was a way to distinguish the Israelites from Canaanites. It was a way to separate themselves.
This ordeal about purity was a way to separate the Isrealites from others. It was a way to set themselves apart as God's chosen people. This was something very important as Israel was forming as they were just one more tribe of people within the Palestine area.
It doesn't quite end there. Up until around after the first Temple fell the religion of the Hebrews wasn't set done in stone. It was more of a monalatry, in that they had one God they worshiped, but recognized that there were other gods. It wasn't until after the temple destruction, and the Jews returning back to Palestine (after being dispersed by first the Assyrians, and then Babylonians) that a more strict view came about.
After the first Temple is destroyed, and the Jews come back, a new revolution begins. And throughout all of that, finding a unity becomes very important. So a new stress is placed on the purity laws as it gives them something that connects them, and sets them apart.
For a really good, in depth look at this, Israel's Ethnogenesis really dives into the earlier time.