Baruch Shem Kevod Malchuto L'olam Va'ed
It was the great sages of Israel who established the many blessings which we have in our liturgy today. Each of these blessings is recited on the very specific occasion for which it was intended. The most common these blessings are the blessings over food and drink which are recited many times each and every day. There are, of course, blessings which are recited when performing various mitzvot, as well. As God's name is considered sacred, mentioning His name without a justified need or out of context is deemed to be sacrilegious and must be avoided whenever possible. This idea is reminiscent of the third of the Ten Commandments which tells us never to take God's name in vain. It is, of course, permitted to address God and use His name in any prayers, even private prayers which are beyond the established liturgy.
Nevertheless, no one is perfect and inevitably one will on occasion recite a blessing that was either in vain or otherwise unnecessary. For example, if one had a cup full of soda but mistakenly thought it was grape juice and recited the blessing "borai pri hagafen", the blessing is considered to be one which was recited in vain. If one realizes his mistake immediately one is permitted to simply recite the proper ending of the blessing, "shehakol neheyeh bidvaro", and then drink. If, however, one did not realize one's mistake until some time had passed, a new blessing must be recited as the original one was in vain, and ultimately, useless.
Another cited case is when one makes a blessing over a fruit, but then before one has a chance to eat the fruit it falls on the floor and is ruined. There too, the blessing one had recited over the fruit is unfortunately now deemed a beracha levatala, a blessing recited in vain, though it was done completely unintentionally.
In the cases above, as in the case of any blessing which was accidentally recited in vain, one must recite "baruch shem kevod malchuto l'olam va'ed" as soon as one realizes in order to remedy one's error. These words, which mean "blessed is the name of His glorious kingdom forever and ever", serve as a means of rectifying having taken God's name in vain. As one will note from its unique wording, the "baruch shem…" makes amends for a blessing recited in vain owing to its exceptional and elaborate way of praising of God. Other authorities contend that "baruch shem…" does not serve in a capacity of making amends, but rather, that it actually retroactively cancels out any damage that may have been caused by a blessing recited in vain.
The phrase "baruch shem…" has other uses and appears in other places as well. The most famous of these appearances is in the twice daily recitation of kriat shema. The practice of saying "baruch shem…" after the "shema yisrael..." originates from Yaakov Avinu who gathered his children together just before he died with the intention of telling them when Mashiach would come. At that moment all his children recited "shema yisrael Hashem Elokeinu Hashem echad" so that their father should be secure knowing that they all believed in and worshipped only Hashem. In response, Yaakov said "baruch shem kevod malchuto l'olam v'aed."
Men who recite a blessing over tefillin which are not kosher have also recited a blessing in vain, as no mitzva is fulfilled. There is also a prominent dispute as to whether one or two blessings are to be recited when putting on tefillin each day. The Ashkenazi custom is to recite two blessings, one for the tefillin of the arm, and one for the tefillin of the head. Nevertheless, in deference to the authorities who hold that the second blessing upon the head tefillin is actually in vain, one is to recite "baruch shem…" after fastening the head tefillin in place. It is interesting to note that in the event that one realizes that one has said numerous blessings in vain, one recitation of "baruch shem…." will suffice to rectify them all.
One will readily notice that the "baruch shem…" is not found anywhere in the Torah, and certainly not among the passages of the shema. Accordingly, since Moshe did not institute the saying of "baruch shem…" when reciting shema, we therefore only say it silently (though the words must be audible to one's ear) out of respect for him. There is another view which teaches that Moshe heard the angels praising God with the formula "baruch shem…", and that he "stole" the idea from them. As such, since this phrase is not truly "ours", having been plagiarized from the angels, we recite it silently. On Yom Kippur, however, when everyone is elevated to the status of angels we recite the "baruch shem..." out loud just like them.
We also find the phrase "baruch shem…" with regards to the Kohen Gadol. When the Kohen Gadol was heard uttering God's name on Yom Kippur, the nation responded with "baruch shem…" We recall this awesome event several times in the Yom Kippur mussaf each year. This is based on a passage in the Torah which tells us that when we hear someone praising God, we ourselves are to join in. We generally accomplish this by responding "amen" to the prayers or blessings of another.
With the exception of Yom Kippur as mentioned, some authorities are of the opinion that "baruch shem..." is a phrase which is always to be recited silently no matter what the context or purpose may be. One who is praying alone need not concern oneself to ensure that one recites the "baruch shem…" silently and it may be recited in one's normal audible manner should one desire to do so.
 Shemot 20:7
 See O.C. 25:5, 206:6
 O.C. 206:6
 Tur O.C. 206
 Rambam Berachot 4:10
 Pesachim 56a
 Rema O.C. 25:5, Mishna Berura O.C. 25:21
 Halichot Shlomo;Tefilla 4 note 24
 Kaf Hachaim 61:47
 O.C. 61:13
 O.C. 619:2, Mishna Berura 619:8
 Yoma 66a
 Devarim 32:3
 Tur O.C. 619
 Teshuvot V'hanhagot 2:46