There are two separate issues of covering the head in Judaism, one for men and one for women.
The basic halacha of covering the head for men is that it is forbidden to walk four cubits (about 8 feet) with an uncovered head. (According to some authorities it is forbidden to go any distance at all without a head covering. It is even considered improper to sit in one's home with an uncovered head.)
It is also forbidden to say a prayer with the name of HaShem or to study Torah without a head covering.
The source for not walking four cubits with the head uncovered is the statement in the Talmud that the mother of Rabbi Nachman bar Yitzchak would not allow him to go with his head uncovered for she said, "Cover your head in order that you should have the fear of heaven upon you." There is another statement that Rav Huna brei d'Rabbi Yehoshua would not go four cubits with his head uncovered.
Another basis given for this practice is to avoid the customs of the non-Jews.
Even though the requirement to cover the head at all times is not stated in the Torah, or even in the Talmud, it is nevertheless an established Jewish custom and is binding on all Jewish men. The requirement to follow established religious customs is based upon the Biblical teaching, "Hear, my son, your Father's instruction, and do not forsake your Mother's Torah" (Mishlei - Proverbs 1:8). It is taught that "Father" in this verse refers to God, while "Mother" refers to the nation of Israel. "Your Mother's Torah" refers to religious customs which were established by the Jewish people. These have the status of Torah and are binding. The requirement of male headcovering is one such custom. (The requirement to cover one's head during prayer is discussed in Mesechta Sofrim 14:15.)
Women who are or have been married (widows and divorcees) are required to cover their hair. A woman who has never been married does not have to.
The Talmud in Kesuvos 72a states that the source for this prohibition is from BaMidbar (Numbers) 5:18 which deals with the laws of a sotah - a suspected adulteress - and states, "The priest shall stand the woman before God and uncover her hair...". Rash'i (Rabbi Shlomo Yitchaki, 1040-1105, author of the primary commentary on the Talmud) provides two explanations for the Talmud's conclusion, one, that from the fact that she is punished midah kneged midah (measure for measure) for exposing her hair to her paramour we see that this is prohibited and, two, from the fact that we expose her hair we see that under normal conditions a Jewish woman's hair should be covered.© Eliezer C. Abrahamson