The Ka’bah (also spelt Kaaba) is a mosque – a Muslim place of worship, built by the Prophet Ibrahim (Abraham), and before him by Adam, the Father of Mankind. It serves as a place of prayer, and the direction which every single Muslim around the world faces to pray.
When you stand to pray, you should face the direction of the Ka‛bah wherever you are. This direction is known as the qiblah.
It is compulsory to face the qiblah during the prayer. This is one of the pillars of prayer, without which the prayer is not valid.
The evidence for doing this is found in both the Qur'an and the Sunnah:
“...So turn your face toward al-Masjid al-Ḥaraam. And wherever you (believers) are, turn your faces toward it (in prayer)...” [The Quran: al-Baqarah, 2:144]
Abu Ḥumaid As-Sā'idī (May Allah be pleased with him) narrated:
"When the Messenger of Allāh (may the peace and blessings of Allāh be upon him) would stand for prayer, he would face the qiblah, raise both his hands, and pronounce the takbeer (i.e., he would say, Allāhu Akbar)." [Ibn Mājah: 803]
As for voluntary prayers, it is not an obligation to face the qiblah when on a riding beast or in a vehicle, although it is recommended to do so at the beginning of the prayer. The Prophet (may the peace and blessings of Allah be upon him) would sometimes pray a voluntary prayer on a moving animal (something which it is not allowed to do for compulsory prayers). He would move the animal to face the qiblah at the beginning of the prayer, but would allow the animal to move freely during the prayer even if it moved away from the qiblah.
Jaabir ibn 'Abdullaah (May Allah be pleased with him) narrated:
"The Prophet (peace and blessings of Allaah be upon him) used to pray voluntary prayers when he was riding without facing the qiblah." [al-Bukhārī: 1094].
A Muslim is required to make their best effort to find the qiblah direction. If they are unsure and there are trustworthy people to ask, then they must ask, or use whatever is available to them, such as a compass, or the direction of mosques in the area. If there is no way to be completely sure, then they must make their best effort.
As for the precise direction of the qiblah, there are two circumstances. If a person is within sight of the Ka'bah itself, then they are required to face the direction exactly, without any deviation. If a person is not in sight of the Ka'bah itself, then it is enough for them to face the general direction, while being as accurate as possible. So, if a person was to miss the direction by a few degrees, but still face the general direction, their prayer would be valid.
Abu Hurayrah (May Allah be pleased with him) narrated that the Prophet (May the Peace and Blessings of Allah be upon him) said:
You can find the qiblah direction for your locality on http://www.salah.com
As for serious mistakes in the qiblah direction (for example praying West instead of East when the qiblah is an Easterly direction), then if the person made their best effort, one of two circumstances apply. If the person realises or is informed by someone during the prayer, they must move immediately to face the correct direction. If the mistake is realised after the prayer has finished, and they made their best effort, then they do not have to do anything.
Jaabir ibn 'Abdullaah (May Allah be pleased with him ) said:
We were with the Messenger of Allah (May the Peace and Blessings of Allah be upon him) on a march or a campaign, and it became cloudy. We tried to determine the qiblah and differed concerning it, so each man prayed on his own, and one of us marked the direction he faced him so that he could check it later. The following morning we looked and found that we had prayed facing a direction other than the qiblah. We told the Prophet (blessings and peace of Allah be upon him) and he did not instruct us to repeat it, and he said: "Your prayer is valid." [al-Hākim: 743]