Muaz Ibni Jabal (May Allah be pleased with him) narrates:

Once, we were with the Noble Messenger (Peace and Blessings be upon him). While we were walking, I asked: “O Allah’s Messenger! Tell me something that would prevent me from entering Hell and would cause me to enter Paradise.”

He replied as follows:

You asked me a difficult thing. However, if Allah bestows what you have asked upon anyone, it will be easy for him.

Worship Allah; don’t associate partners with Him; perform prayers by complying with its rules and conventions; pay zakat (alms); fast in the month of Ramadan; visit the House of Allah (go to Makkah for pilgrimage).

After that the Prophet uttered ”O Muaz, do you want me to show you the gates of goodness.

I replied:” Certainly, O Allah’s Messenger”

He said:

Fasting is a shield and a shelter against evil.

Just as water extinguishes fire, so does charity extinguish sins.

Praying in the middle of night is the sign of good servants.
” (Tirmidhi, Eeman (Faith): 8)

In the past, wars had been fought by means of swords and shields, arrows and spears had also been used. A warrior held a sword in one and a shield in the other. While he was attacking with his sword, he was also confronting the counter attacks by his shield. Thereby he had protected his life by means of his shield, and saved from getting hurt.

Now, fasting is the most powerful immaterial weapon against spiritual dangers, sins and evils. For the one who uses fasting shield until the time of breaking the fast and who takes fasting as a shelter, gets ready for evil arrows shot by soul and devil and protects his heart from getting hurt.

Sins are committed on account of obeying what emotions and desires want. When sexual desires are broken by means of starvation, thirst, etc, man restrains himself and therefore avoids sins.

When he avoids sins, he gets protected from the torment of Hell in the Hereafter which is the result of sins.

The Prophet (PBUH) utters in another Hadith:

Fasting is a shield protecting man from Hell. It is like the shield which protects you from death in wars. (Nasai, Savm (Fasting): 167)