Regarding the timing and contextual background of the revelation (asbāb al-nuzūl), it is a "Meccan surah", which means it is believed to have been revealed in Mecca, instead of later in Medina. It was revealed around 2-3 years before Hijrah, in a later stage of Muhammad preaching in Mecca.
The surah states that Nuh, Ibrahim, Lut, Shuaib, Hud, Saleh, Musa and Muhammad all were prophets of God. All of them endured hardships. For example, Noah was ridiculed often and Abraham was thrown into the fire. But God destroyed their people who transgressed. Verse 40 says
So each We punished for his sin; of them was he on whom We sent down a violent storm, and of them was he whom the rumbling overtook, and of them was he whom We made to be swallowed up by the earth, and of them was he whom We drowned; and it did not beseem Allah that He should be unjust to them, but they were unjust to their own souls.[2]
The parable of those who take protectors other than God is that of a spider spinning a shelter. And the flimsiest of all shelters is certainly that of a spider, if only they knew.
Mustafa Khattab, author of the Clear Quran, notes that "Externally, the web is too flimsy to protect the spider against rain and strong wind. Internally, the spider’s family structure is fragile, since some species are cannibalistic, with the female preying on the male and the young eating their own mother."[5]
"This is how Allah described the idolators in their reverence of gods besides Him, hoping that they would help them and provide for them, and turning to them in times of difficulties. In this regard, they were like the house of a spider, which is so weak and frail, because by clinging to these gods they were like a person who holds on to a spider's web, who does not gain any benefit from that. If they knew this, they would not take any protectors besides Allah. This is unlike the Muslim believer, whose heart is devoted to Allah, yet he still does righteous deeds and follows the Laws of Allah, for he has grasped the most trustworthy handle that will never break because it is so strong and firm."
^Wherry, Elwood Morris (1896). A Complete Index to Sale's Text, Preliminary Discourse, and Notes. London: Kegan Paul, Trench, Trubner, and Co. This article incorporates text from this source, which is in the public domain.