The Code of Jewish Law states:
"One should have in mind the meaning of the words when he says blessings." (Shulchan Aruch 5:1)
The commentary of the Mishna Brura states:
This complies with what the Sages, of blessed memory, said that one should not cast the blessing out of his mouth. (Berachot 47a) Instead, one must have the meaning in mind when he says the blessing and should say the blessing at leisure....he should apply his heart to the blessing so that he says it for the sake of his Creator….
When one makes a blessing over the washing of his hands or makes a blessing over fruit or over the performance of a mitzvah, which is commonly uttered by everyone, he should apply his heart to the blessing so that he says it for the sake of his Creator, Who was very kind to him and gave him the fruit to enjoy and commanded him regarding the performance of the mitzvah.
One should not conduct himself like a person who acts out of habit and pronounces words with his mouth without contemplating them in his mind. For this behavior, G‑d's anger flared up against His people, and He sent to inform us through Isaiah: 'Because of the fact that this people has approached, with this mouth and these lips it has honored Me and its heart is far from Me.' (Isaiah 29:13) See further in the Code of Jewish Law the extensive discussion of the gravity of the punishment for this.
When one mentions the Divine Name [Havayah], he should have in mind the meaning of the Name as it is pronounced ["Ado-nai"], referring to His Lo-dliness, i.e. he should have in mind that He is the L-rd of all Creation. [This is because it is forbidden to read the honored and awesome name Havayah as it is written.] This accords with what the Sages, of blessed memory, said: "If one pronounces the Divine Name with its actual lettering, he will not have share in the World to Come." (Sanhedrin 90a)
Instead, one must read it as if the name Ado-nai is written, also with the vowels appropriate to the name Ado-nai, i.e. the letter alef with a chataf patach (but not with a patach alone or with a sheva alone), the letter dalet with a cholam, and the letter nun with a kamatz. One must stress the letter yud so that it is clearly discernable. One should have in mind only mentally the Name which refers to His external existence, but it should not influence one's pronunciations. The stress for its intonation is on the final syllable.
He should also have in mind the meaning of the Name as it is written, the letters yud, hei, etc., meaning "He was, is and will always be in existence".
When one mentions the divine name Elokim, he should have in mind that He is overpowering, omnipotent and almighty.
The Biur Ha-Gra writes that deep analysis indicates that it is only necessary to have in mind the meaning of the Divine Name [Havayah] as it is read [Ado-nai]. This is because in all instances we follow the version to be read and not the version which is written, although the written version embodies major secrets. This applies whenever the Divine Name is mentioned, except to the Reading of the Shema, for which it is also necessary to have in mind "that He was, etc." See there.
In a place where the divine name written is spelled "Ado-nai", all authorities are agreed that one is only required to have in mind that He is the Lord of all Creation.
When a person prays, he must…be conscious that the Shechinah is opposite him….
If someone who is relating the kindness which G‑d performed for him, mentioned the Divine Name in the beginning and wishes to finish his account of what G‑d performed for him, it is forbidden to interrupt him with speech in case he will not finish his account as result of this and he will thus have mentioned the Divine Name in vain. However, if someone wishes to curse anybody and uses the Divine Name at the beginning, it is a mitzvah to interrupt him so that he will not finish the words which he wishes to say and will not come to sin. (Code of Jewish Law, quoted in the Chudushei R.A.E.)
Regarding proper intent during prayer, the Code of Jewish Law continues:
When a person prays, he must apply his mind to the meaning of the words which he pronounces with his lips and be conscious that the Shechinah is opposite him. (Shulchan Aruch 98:1)
When he prays, a person should not have in mind the significance of the Divine Names and professions of His unity, but should merely pray simply, noting the implication of the words with the application of his heart. That is unless that person has entered into the secrets of G‑d [Editor's emphasis] and is able to have such matters in mind with his heart and soul and fear of Him. Otherwise, Heaven forbid, one will cause grave damage if he does so. See the commentary of the Magen Avraham, who cites the Zohar. In the responsa of the Rashal, Sec. 98, the author writes about this at length and testifies that, even after studying the secrets of Kabbala, the Rash said that he prayed like a day old child.
See the Penei Yehoshua at the beginning of the chapter Eyn Omdin (Berachot, Chapter 5). He writes there that the thoughts which one is required to have in mind that are described here in the Shulchan Aruch cannot be had in mind when one is actually praying, but only beforehand. During the prayer itself, however, one must merely have in mind the meaning of the words. See there. (Mishna Brura)
One should remove all extraneous thoughts which preoccupy him, until his thoughts and application remain devoted purely to his prayer. He should bear in mind that if he would be speaking before a king of flesh and blood, he would set out his words and say them with painstaking application, so as not to stumble. This is therefore all the more necessary before the King, King of kings, the Holy One, blessed be He, Who investigates every thought.
This is how the pious and men of virtuous deeds would act: They would seclude themselves and apply themselves to their prayer until they reached the stage when they had divested themselves of the material, and the intellectual faculty prevailed, to the extent that they came close to the level of prophecy.If a foreign thought comes to one during the prayer, one should be silent…
If a foreign thought comes to one during the prayer, one should be silent until the thought has ceased. One must reflect on matters that subdue the heart and direct it towards one's Father in Heaven and should not think of matters which involve light headedness. (Ibid.)
In the Sefer Ha-Gan, the author writes that in order to banish a bad thought when one prays, one should say three times "Pi" [spelled peh-yud].(These are the first letters of the names "Palti" and "Yosef" (Hebrew for "Joseph"), who prevailed over the inclination of their hearts, as stated in Sanhedrin 19b and 20a). Following that, he should spit three times. He should not spit fully, but in a gentle manner, with the tongue in between the lips while he spits. The thought will definitely go away if one does so. The Magen Avraham writes about this that it does not appear to him right to act like this in the middle of the Standing Prayer, for it constitutes an interruption and who knows whether it is a tested cure.
In the work Eliyahu Rabba, the author cites from the abridged Shelah a remedy for removing an extraneous thought before one prays: One should pass his right hand over his forehead three times and say each time, "A pure heart create for me G‑d and an upright spirit renew inside me". (Psalms 51:12) Likewise, if an extraneous thought comes to one in the middle of the prayer, he should be silent a little and then pass his right hand over his forehead and think the aforementioned verse.