All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
There are some details the scholars mentioned regarding these issues.
As for the Qiblah, if the person is traveling and he could not find the direction of the Qiblah, then he prays according to his Ijtihaad [what he predominantly believes to be the direction of the Qiblah] and he does not have to repeat the prayer even if he finds out later that he was wrong.
However, if he was able to know the direction of the Qiblah for certain, by someone informing him about it, or by him looking at the direction of the niches (in mosques) in towns (and thus knowing the direction of the Qiblah); in this case he must pray in that direction and it is not permissible for him to make Ijtihaad in this case. Hence, if he prays towards any direction other than the direction of the Qiblah without asking the people to inform him about it, then he is negligent about his obligation; consequently, he must repeat it.
Therefore, if he prays and then he finds out that he was wrong, then he must repeat the prayer in all cases, except in the first case we mentioned, because facing the direction of the Qiblah is part of doing the order and it is a condition (for the validity of the prayer), so it must be fulfilled.
As for avoiding the impurity; it is also one of the conditions for the validity of the prayer. The scholars differed in opinion about the one who finds impurity on his clothes after he had performed the prayer, and he did not know about it, or that he knew about it but forgot it. Some of them are of the view that he must repeat the prayer. The correct opinion; however, is that he is not obliged to repeat the prayer because he is excused due to ignorance and forgetfulness.
The difference between this condition and other conditions, like ritual purity and facing the direction of the Qiblah, is as we have already mentioned, that this condition, which is avoiding the impurity, is part of abandoning what is forbidden, contrarily to other conditions, which are part of fulfilling a command.
Indeed, the Sharee’ah is lenient in matters where one must abandon what is forbidden, but it is not in the cases where one has to fulfil commandments.
Shaykh Ibn 'Uthaymeen said:
“The most preponderant opinion is that if a person prays with impurity without knowing of its presence or forgot that it was there, then his prayer is valid. This is like if his garment is stained with impurity and he neglected it (by not washing it right away) and then he prayed while forgetting that it was there, then his prayer is valid, as Allah says (what means): {Our Lord, do not impose blame upon us if we have forgotten or erred.} [Quran 2:286]
The same thing applies if he was unaware of it and did not know about it until after he had prayed, then his prayer is also valid for the previous verse [..] If a person prays without Wudhoo' (ablution) out of forgetfulness, then he must repeat the prayer, such as if his Wudhoo' is invalidated and the prayer time comes and he prayed without Wudhoo', forgetting that his Wudhoo' was invalidated, and then he remembered later, then he must repeat the prayer after performing Wudhoo'.
Also, if he is invited to a banquet and he eats meat not knowing what type of meat it is, and he prays and then discovers after he had prayed that it was camel’s meat, then he must perform Wudhoo' and repeat the prayer even if he did not know when he ate it that it was camel’s meat. Hence, the one who prayed while forgetting that he was without Wudhoo' must repeat the prayer, and the one who prayed while forgetting that he had some impurity (on his body or clothes), or did not know that he was in such a state is not obliged to repeat the prayer. The difference between the two is that in regard to the issue of not being in a state of Wudhoo', it constitutes abandoning a command, and abandoning a command out of forgetfulness or out of ignorance exempts one from sin, but it does not exempt him from repeating the prayer or any other act of worship, in a correct and valid manner, as he is able to do so and make up for the deficiency.
However, the one who prays in an impure garment out of forgetfulness or ignorance, then this constitutes of doing something forbidden. Doing what is forbidden out of forgetfulness or ignorance exempts him from sin because of his forgetfulness or ignorance, and it becomes as if he did not do what is forbidden. Hence, this exemption from sin because of forgetfulness or ignorance renders his prayer valid, as if he did not do what is forbidden (i.e. as nothing affected the validity of his prayer at all).” [End of quote]
Based on the above, the difference between abandoning a command and doing what is forbidden becomes clear to you, and that if someone abandons a command out of forgetfulness or ignorance, he is required to repeat his worship even though he is not sinful, contrarily to doing what is forbidden.
Allah knows best.