Abu Dharr, may Allah be pleased with him, said, ‘The Messenger of Allah, peace and blessings of Allah be upon him, said:
“Allah continues to turn towards His slave whilst he is praying, so long as he does not turn away, but if he turns away, [Allah] turns away from him.” (Recorded in Abu Dawud no.909)
Turning away is of two types:
i) The turning away of the heart to something other than Allah.
ii) The turning away of the eyes.
Both of them are not allowed, and are detrimental to the reward for the prayer. The Messenger of Allah, peace and blessings of Allah be upon him, was asked about turning away during the prayer, and he said:
“It is something that Shaytan steals from a person’s prayer.” (Recorded in al-Bukhari, Kitab al-Azan, Bab al-Iltifat fi’l-Salah)
The one who turns away with their heart or their eyes during the prayer is like a man who is called by the ruler and made to stand before him, and when the ruler starts to address them, they turn away, looking left and right, not listening to what the ruler is saying and not understanding a word of it, because their heart and mind are elsewhere. What does this person think the ruler will do to them?
The least that they deserve is that when they leave the ruler, they are hated and no longer valued. One who prays like this is not equal to one who prays with the proper presence of mind, turning to Allah in prayer in such a way that they feel the greatness of the One before Whom they are standing, filled with fear and submission; they feel too shy before their Lord to turn to anyone else or to turn away.
The difference between their prayers is as Hassaan ibn ‘Atiyah said: “The two men may be in one congregation, but the difference in virtue between them is as great as the distance between heaven and earth. One of them is turning with all his heart towards Allah, whilst the other is negligent and forgetful.” (Al-Waabil al-Sayib by Ibn al-Qayyim, Daar al-Bayaan, p. 36).
As for turning away for a genuine reason, this is OK. Abu Dawood reported that Sahl ibn al-Hanzaliyyah said: “We started praying – Salaat al-Subh (Fajr) – and the Messenger of Allah (peace and blessings of Allah be upon him) was looking at the ravine.” Abu Dawood said: “He had sent a horseman at night to guard the ravine.” This is like when he carried Umaamah bint Abi’l-‘Aas, and when he opened the door for A’ishah, and when he came down from the minbar whilst praying in order to teach them, and when he stepped back during Salaat al-Kusoof (prayer at the time of an eclipse), and when he grabbed and strangled the Shaytaan when he wanted to interrupt his prayer. He also ordered that snakes and scorpions should be killed even during prayer, and a person who is praying should stop and even fight one who wants to pass in front of him whilst he is praying. He told women to clap during prayer [if they spot a mistake on the part of the imaam], and he used to wave or gesture to people who greeted him whilst he was praying. These and other actions may be done in cases of necessity, but if there is no necessity, then they are just idle gestures that cancel out khushu and are therefore not allowed during prayer. (Majmoo’ al-Fataawa, 22/559).