Oh boy, it's complicated, but I hope this crash course sheds some light on the issue!
First, to dispel a commonly held misconception, Islamic doctrine does not condone or encourage terrorism. There are better scholars on this sub with more knowledge of Islamic history, so I will leave discussion of the dogma to them. What I'm seeking to answer with this question is why in our current historical moment, the most active and (seemingly) most violent terrorist groups have claimed Islam as their cornerstone. A full discussion of this would violate the 20-year rule, so I'm just going to go back to the roots and talk about the events that inspired it.
European Colonialism
We would not be able to properly understand the struggles in the Middle East and the Muslim world at large if we don't delve into the colonial conquests into the region in the late 19th and early 20th centuries. Britain, France, Russia, and to a lesser extent, Italy, all invaded Middle Eastern, North African and South Asian regions that were majority Muslim. Britain held Egypt and India, France held Algeria, Morocco and Tunisia, Russia went into the Caucasus and Iran, and Italy held Libya. As is the case with most imperial conquests, the invaders declared themselves superior and subjugated the native populations. There was a clear line between Christian invaders and Muslim subjects, and as you may imagine, things didn't go well. For example, British officer Frederick Cooper, Deputy Commissioner of Armitsar, reported in 1857 about quieting an uprising in Lahore, India by Sepoys (Bengali soldiers in what is now modern day Pakistan):
By midnight, as the glorious moon came out through the clouds and reflected herself in myriad pools and streams, we had gathered 282 of the Bengali rebels. In the morning, a party of Sikhs arrived with a large supply of rope. But being as the trees were scarce, the rope was not used. A larger problem lay in dealing with the loyal Mohammedan troopers, who would surely not have stood by in silence as justice was meted out upon their rebellious co-religionists. As fortune would have it, the 1st of August was the anniversary of the great Mohammedan festival of Bukra Eid. A capital excuse was thus afforded to permit the Mohammedan horsemen to return to their homes to celebrate, while we Christians, unembarrassed by their presence and aided by the faithful Sikhs, might perform a ceremonial sacrifice of a different nature upon their brethren.
...
At first light, the prisoners were bound together in groups of ten and brought out of their prisons. Believing they were about to be tried and their unwarranted grievances heard, the Sepoys were unusually docile. But when the shots began to ring in the still morning air, and they suddenly discovered the real and awful fate that awaited them, they were filled with astonishment and rage.
The execution commenced uninterrupted until one of our men swooned away (he was the oldest of our firing-party) and a little respite was allowed. After we had shot some 237 of the Mohammedans, the district officer was informed that the remaining captives were apparently refusing to come out of the bastion, where they had been imprisoned temporarily in expectation of their execution. Anticipating a rush and resistance, preparations were made against their escape. The bastion was surrounded, the doors opened, and behold! Forty-five bodies, dead from fright, exhaustion, fatigue, heat, and partial suffocation, were dragged into the light. These dead, along with their executed comrades were through by the village sweepers into the well. Thus, within forty-eight hours of their escape, the entire 26th regiment was accounted for and disposed of.
To those of you fond of reading signs, we would point to the solitary golden cross still gleaming aloft on the summit of the Christian church in Delhi, whole and untouched; though the ball on which it rests is riddled with shots deliberately fired by the mutinous infidels of the town. The cross symbolically triumphant over a shattered globe! How the wisdom and heroism of our English soldiers seem like mere dross before the manifest and wondrous interposition of Almighty God in the cause of Christianity! (Account printed in Reza Aslan's No god but God: The Origins, Evolution, and Future of Islam, 226-227).
So the concept of those uppity Muslims who just can't be happy with their lot in life is not new by a long shot. The term "terrorist" hadn't been coined yet, but the sentiment expressed here with the term "rebels" is the same.
Coinciding with the European colonial period was also the decline of the Ottoman Empire, the last Islamic Caliphate. It was officially dissolved in 1924 by Turkish leader Mustafa Kemal Ataturk, at which point Turkey as a country went through massive reforms to secularize and be accepted as a legitimate power by Europe (with only lukewarm success, but that's another topic).
Then, around mid-20th century, the colonies held by the European imperialists began to gain independence, and were essentially facing a few options in regards to nation-building and modernity: does each country follow Turkey's example, abandon their Islamic history, and try to model themselves after Western democracy, or is there a way to rectify both Islamic governance and the modern nation state? Each country faced with this decision tackled it differently, with varying degrees of success. But the roots of the modern ideology of groups like Al-Qaeda and ISIS were originally born out of this dilemma.