Our Gemara on Amud Aleph states an aphorism from Shmuel, which also has halakhic ramifications as it might cause us to doubt somebody's lineage:
"If one habitually claims that others are flawed, he disqualifies himself with his own flaw. The flaw he accuses them of having, is in fact, the one that HE has."
This psychological and spiritual process is one of the ego defenses that Freud identified. Briefly, an ego defense is an unconscious psychological mechanism that protects the personality and sense of self from ideas, thoughts, or feelings that are too dangerous or threatening. In this case, the inadequacy and guilt that the person feels about himself needs some type of discharge. (It is an observed idea about human nature already known to the ancients, that thoughts and feelings represent a form of energy that builds up in the person. This makes sense scientifically as well, as thoughts and feelings are transmitted electrically through the nervous system, and one feels tension building up.) However, since the person does not want to admit to himself that he has this deficiency, he has to contend with this urge to correct it in some way. Partially repressing it causes it to come out by pointing out other people's flaws. And it is even more likely to be activated when he sees his own flaw in somebody else because it relieves the sense of inadequacy and guilt and puts it upon another.
Baalei Musar, mystics, and Chasidim have also written about this human process, and I will quote a few insights that are particularly insightful.
Orchos Tzaddikim (25) cautions that often our strongest urges to engage in l’shon hara stem from this dynamic. There is something we see in the other person that irritates us greatly. However, what really irritates us is that what we see in this person reminds us of ourselves. We can check that urge to slander and criticize by asking ourselves, "Whom are we really angry at?"
The Baal Shem Tov (Bereishis 126) cleverly re-words a Rabbinic teaching, which was already reworded once before. The original teaching comes from Mishna Negayim (2:5):
כָּל הַנְּגָעִים אָדָם רוֹאֶה, חוּץ מִנִּגְעֵי עַצְמוֹ.
"All negaim (lesions or blemishes) may be examined by a person, except his own."
Originally, this was merely a legal statement. A Cohen who is normally empowered to rule about skin lesions, if they are considered tzoraas or not, is not authorized to do so for a lesion that is on his own body or possessions.
The popular Rabbinic quip is to read the statement here in a non-legal sense. Meaning to say, "A person sees everyone else's blemishes (and shortcomings), save for their own." Now, the Baal Shem Tov's clever twist on this is that we read it as follows: "All blemishes and imperfections one sees in outsiders (חוּץ means except, but also outside) are really his own. Everything he sees and criticizes are a product of his inner imperfections.
Finally, Rav Tzaddok (Takanas Hashavin 10:1) offers a spiritual-psychological explanation for how this works but different from the Freudian ego defense. According to Rav Tzaddok, this is a matter of perception and confirmation bias. We automatically look to confirm and compare the unfamiliar with the already familiar. Thus, when in doubt, we tend to judge the motives and behaviors of others in accordance with patterns of thoughts and feelings indigenous to our personalities. If one steals, one will obviously be mistrustful and therefore accuse others of stealing. On the other hand, if one is kind, generous, and honest, he will interpret behavior, thoughts, and motivations along those positive and pro-social lines.