You would need an environment where some form of predator needs a new niche in the food chain. Perhaps some other form of predator decimated the prey of a certain size very much and what's left is either too big or too fast for our ancestor creature.
It could just become extinct. But there might be mutations that manage to survive, now being the fittest to survive a new race develops from our ancestor.
One such mutation might be the ability to spit/launch strands of web-like substances at prey that would otherwise be too quick or too dangerous to our predator.
In the same vain a prey creature could develop it as a defensive weapon to drive off predators like Bombardier beetles do with their defensive secretions.
It seems that the slime produced by Hagfish has fibres in it that resemble spider silk (I could find this resemblance mentioned in the German wiki entry but not in the English one, where they only mention "hagfish slime filament protein" as being researched). This indicates that larger creatures and such of different species can use similar materials for defence.
Just for reference the German passage mentioning it:
Kürzlich wurde zudem entdeckt, dass der Schleim, den Schleimaale abgeben, strukturell einzigartig ist, da er reißfeste, fadenförmige Fasern enthält, die chemisch gewisse Ähnlichkeiten mit denen der Spinnenseide aufweisen.
Schleimaale = Hagfish / Spinnenseide = Spidersilk
I hope this doesn't violate the English speaking nature of this SE.
Loosely translated it means: The slime secreted by hagfish is structurally unique as it contains tear-resistant threadlike fibres that have a certain chemical similarity to those of spider silk.