In the name of Allah, Most Gracious, Most Merciful
Allah said in Surat al-Isra, Ayah 36: which means: Do not act or give judgments in the Religion without knowledge. On the Day of Judgment ones ears, eyes, and heart will be asked about their deeds.
And do not pursue that of which you have no knowledge. Indeed, the hearing, the sight and the heart - about all those [one] will be questioned. 17:36
The Prophet, sallallahu ^alayhi wa sallam, also advised in this matter. at-Tirmidhiyy narrated that the prophet said: These words of the Prophet mean: Say something good or else keep silent. This advice is important to follow because giving judgments in the Religion without knowledge is among the ways which lead a person to be a loser in the Hereafter. Indeed, this matter is among the top reasons leading people to enter Hellfire. The one who says something is halal permissible or haram forbidden in the Religion without relying on a valid religious permission to do so is sinful and deserves the severe torture in Hellfire
The pious, knowledgeable people said it is a very good act for the person to say:I do not know, when it is applicable to do so. If one does not know the religious judgment on a particular case, he should not venture his own opinion on it. Once, when Imam ^Aliyy was asked about something which he did not know, he said, I do not know. Then he added, How restful it is for me to say I do not know when I do not know. Imam ^Aliyy meant that he felt comfortable not to answer a religious question when he did not know the answer to it. We should take heed and follow the words of Imam Aliyy in this matter. He was one of the most honored Companions of the Prophet--known for his high level of knowledge and comprehension in the Religion. Moreover Once, when Prophet Muhammad, sallallahu ^alayhi wa sallam, was asked what are the best and the worst places, he answered: His words mean: I do not know. I will ask Jibril. When the Prophet asked Jibril, Jibril also said, I do not know. After a period of time, Jibril returned to Prophet Muhammad with the answer which Allah revealed to him: The worst places are the markets and the best places are the mosques. This is an authentic hadith related by Imam Muslim and others.
The Prophet, sallallahu ^alayhi wa sallam, taught us a very important lesson by his response. It is not shameful for one to reply, I do not know when it is applicable, but it is shameful to speak about religious matters without knowledge. Only Allah knows everything. For the Muslim to be safe on the Day of Judgment, he should speak only about what he knows. If one needs an answer to a Religious question, he should ask a trustworthy knowledgeable Muslim. This is the way the knowledge of the Religion is taught, and this is the way the knowledge is preserved.