Praise be to Allah.
Shaykh al-Shanqeeti (may Allaah have mercy on him) said:
Note that Allaah mentions this dog in His Book, and says that he was stretching forth his two forelegs at the entrance to the Cave [al-Kahf 18:18], in the context of referring to their high status. This indicates the great benefit to be gained by keeping company with righteous people. Ibn Katheer (may Allaah have mercy on him) said in his commentary on this aayah:
“Their dog was included in their blessing, so he slept as they slept in that situation. This is the benefit of keeping company with righteous people, so this dog was mentioned and was given status too.”
This meaning is also indicated by the hadeeth of the Prophet (peace and blessings of Allaah, in which he said to the one who said that he loved Allaah and His Messenger, “You will be with those whom you love.” (Agreed upon, from the hadeeth of Anas).
From this it may be understood that keeping company with bad people causes a great deal of harm, as Allaah explained in Soorat al-Saaffaat, where He says (interpretation of the meaning):
“A speaker of them will say: Verily, I had a companion (in the world),
Who used to say: Are you among those who believe (in resurrection after death).
(That) when we die and become dust and bones, shall we indeed (be raised up) to receive reward or punishment (according to our deeds)?
(The speaker) said: Will you look down?
So he looked down and saw him in the midst of the Fire.
He said: By Allaah! You have nearly ruined me.
Had it not been for the Grace of my Lord, I would certainly have been among those brought forth (to Hell)”