Why does the Gemara tell us that we can keep money or property when a non-jew makes a mistake?
The question of Ta'us Akum has been the subject of much consideration. There is plenty of argument about it as far back as the Tana'im and throughout the Rabbinic literature. Let me, however, make a few salient points.
1) Even those who are of the permissive view (which is, in fact, the final Halachah as set out in the Rema, Choshen Mishpat 348:2) refer to Ta'us Akum . "Akum" means pagan idol worshippers who were unprincipled ruffians. It certainly excludes Muslims, Protestants, and most of the Western world today. (As to Catholics, this could depend on the Machlokes Rambam and Rabeinu Tam as to their status in view of their belief in the concept of the Trinity.)
2) There is the well-known words of the Me'iri in Bava Kama (37b), who writes, "That which is said in the Gemara (regarding the exemption of the owner of a Shor which gores a Shor of an Akum) applies only to those peoples that do not conduct themselves in the ways of ethical behavior and proper etiquette, such as the Gemara says about them, 'See the B'nei Noach who accepted upon themselves the Seven Mitzvos and who do not fulfill them, that their money was made permissible [to take].' This [allowance], however, applies only when they themselves act in a way deserving of it. But when they are fulfilling their Seven Mitzvos, then their law with regard to us is the same as our law with regard to them, and no favoritism in judgment is to be shown to ourselves. And now, it goes without saying [that this certainly applies] with regard to the nations that conduct themselves in the ways of ethical behavior and proper etiquette." A similar statement is cited by the Shitah Mekubetzes in the name of the Me'iri, and cited in the margins of the Gemara in Bava Kama 113a.
Thus, even Catholics (and perhaps also Hindus, atheists, etc., who are clearly Akum) are nonetheless within this definition of those who "conduct themselves in the ways of ethical behavior and proper etiquette." (Conversely, it might exclude a people like the Taliban and Palestinian terrorists!)
3) The permission does not apply where there is any chance of Chilul Hash-m (and the chances of this in modern conditions must be greatly increased).
4) It is clearly forbidden to provoke the error by the Nochri, such as by misleading him. Genevas Da'as is strictly prohibited (Chulin 94a, Choshen Mishpat 228:6), or to trick him in any other form of dishonest business practice.
5) It only applies where the Nochri has made the error entirely of his own volition, and the Jew says that he is relying on the Nochri's computation (see Rema, Choshen Mishpat 348:2: "and there are those who say that it is Asur to cause him to err, but if he erred on his own that it is permitted [to keep the overpayment]").
6) There is also a positive aspect of Kidush Hash-m involved, especially nowadays, where if by returning the overpayment error, one can create a positive Kidush Hash-m, one should do so.
See also in this context the famous words of the Be'er ha'Golah in Choshen Mishpat 348:4-5, which is quoted with great approval by the Tiferes Yisrael (Bava Kama 4:4, Boaz 1 (Yachin 17)): Anyone who steals, even [the minimal amount of] the value of a Perutah, transgresses the Torah prohibition of stealing... whether he steals from a Jew or from an Akum. The Ta'us of Ovdei Kochachim Anasim ha'Kadmonim (the early Akum who were extortionists) -- such as to cause him to make an error in calculation or to be Mafki'a his Halva'ah, did not involve an Isur, but only when there was certainly that there would be no Chilul Hash-m involved; and there are those who rule that causing him to make an error is Asur, and only if he made an error on his own, such as an error in calculation, and the Jew says to him, 'I am relying on your calculation.' But where there could be a Chilul Hash-m, it is Asur Min ha'Torah. And the great sages have written that they have seen with their own eyes that those people who profited from their (i.e. Akum's) error did not have any success and they eventually lost all of their money and assets, and they left behind them no blessing. While, on the other hand, many people were Mekadesh Shem Shamayim and they returned the money of an Akum received in error, even when it was a significant amount, and those people became great and became immensely wealthy and successful, and they left much to their heirs...."
This is incorporated in the final Halachah as ruled by the Aruch ha'Shulchan in Choshen Mishpat 348:2 (who is accepted by the Lithuanian Jewry as having the final say on matters outside of Orach Chaim). See there, who cites the Be'er ha'Golah, and who concludes, "And in these times, in all of these countries, it is Asur Min ha'Torah, forbidden by the Torah, to cause any error or do any trickery, and there is absolutely no difference between a Jew and a Nochri in this regard."
An example of this in Chumash is Yakov Avinu (Bereishis 43:12), who told his son to give the money back to Yosef, saying, "Perhaps it is a mistake," even though it was certainly Mutar to keep the money because of Hafka'as Halva'ah, as the Egyptians were idolaters within the definition of Akum.
I hope the above assists.
Kol Tuv,
Joseph Pearlman