Ibn Abi Hatim recorded that Ibn `Abbas said, "Those who violated the sanctity of the Sabbath were turned into monkeys, then they perished without offspring.'' Ad-Dahhak said that Ibn `Abbas said, "Allah turned them into monkeys because of their sins. They only lived on the earth for three days, for no transformed person ever lives more than three days. They did not eat, drink or have offspring. Allah transformed their shapes into monkeys, and He does what He wills, with whom He wills and He changes the shape of whomever He wills. On the other hand, Allah created the monkeys, swines and the rest of the creation in the six days (of creation) that He mentioned in His Book.''
(So We made this punishment an example) means, Allah made the people of this village, who violated the sanctity of the Sabbath,
(an example) via the way they were punished. Similarly, Allah said about Pharaoh,
﴿فَأَخَذَهُ اللَّهُ نَكَالَ الاٌّخِرَةِ وَالاٍّوْلَى ﴾
(So Allah, seized him with punishing example for his last and first transgression) (79:25). nAllah's statement,
﴿لِّمَا بَيْنَ يَدَيْهَا وَمَا خَلْفَهَا﴾
(for those in front of it and those behind it) meaning, for the other villages. Ibn `Abbas commented, "Meaning, `We made this village an example for the villages around it by the manner in which We punished its people.''' Similarly, Allah said,
﴿وَلَقَدْ أَهْلَكْنَا مَا حَوْلَكُمْ مِّنَ الْقُرَى وَصَرَّفْنَا الاٌّيَـتِ لَعَلَّهُمْ يَرْجِعُونَ ﴾
(And indeed We have destroyed towns (populations) round about you, and We have (repeatedly) shown (them) the Ayat (proofs, evidences, verses, lessons, signs, revelations, etc.) in various ways that they might return (to the truth and believe in the Oneness of Allah ـ Islamic Monotheism)).(46:27)
Therefore, Allah made them an example for those who lived during their time as well as a reminder for those to come, by preserving their story. This is why Allah said,
(and a lesson for Al-Muttaqin (the pious)), meaning, a reminder. This Ayah means, "The torment and punishment that this village suffered was a result of indulging in Allah's prohibitions and their deceit. Hence, those who have Taqwa should be aware of their evil behavior, so that what occurred to this village does not befall them as well.'' Also, Imam Abu `Abdullah bin Battah reported that Abu Hurayrah said that the Messenger of Allah said,
«لَا تَرْتَكِبُوا مَا ارْتَكَبَتِ الْيَهُودُ فَتَسْتَحِلُّوا مَحَارِمَ اللهِ بِأَدْنَى الْحِيَل»
(Do not commit what the Jews committed, breaching what Allah has forbidden, by resorting to the lowest types of deceit.)
This Hadith has a good (Jayid) chain of narration. Allah knows best.
﴿وَإِذْ قَالَ مُوسَى لِقَوْمِهِ إِنَّ اللَّهَ يَأْمُرُكُمْ أَن تَذْبَحُواْ بَقَرَةً قَالُواْ أَتَتَّخِذُنَا هُزُوًا قَالَ أَعُوذُ بِاللَّهِ أَنْ أَكُونَ مِنَ الْجَـهِلِينَ ﴾
(67. And (remember) when Musa said to his people: "Verily, Allah commands you that you slaughter a cow.'' They said, "Do you make fun of us'' He said, "I take Allah's refuge from being among Al-Jahilin (the ignorant or the foolish).'')