The majority of modern fantasy is set in an alternate versions of Europe's Middle Ages, complete with knights, castles, and swords. North America and Down Under, which have no castles but whose culture is European-derived, produce large numbers of fans of medieval-style fantasy--and a fair number of writers of such (including me).
I'm often asked, "How much research do you have to do to write medieval-themed fantasy?" Well, in a way I've been doing the research my entire adult life. As I've discussed previously, I've somehow managed to balance being both a fantasy writer and a professor of medieval history, with a decent collection of books I've written in both areas.
And of course I've used a lot of motifs from real medieval history in my stories; see the discussion of an example here.
Fantasy as its own genre got off the ground in the 1950s-70s, indubitably due to JRR Tolkien's Lord of the Rings. Tolkien himself was a professor of medieval literature, so it is not surprising that his own fiction was imbued with the tropes that medieval authors put into their own literature--almost all of which we would now label "fantasy." (For them, it was just literature.)
Once people found how compelling Lord of the Rings was (and realized it was not "super science fiction," as an early review of LotR stated), they started looking for more, and discovered such earlier works as CS Lewis's Narnia series, Lord Dunsanay, and a few others. Then people started writing their own, and they haven't stopped.
These days, fantasy is a mix of several quite different sources. One of course is Tolkien himself. There are plenty of stories where an ill-assorted group of comrades set off on a dangerous quest to overcome the Dark Lord, even though most of these stories end up having the comrades find the powerful magic artifact to whack said Dark Lord, rather than following Tolkien, whose heroes sought to destroy such an artifact.
Once one gets past imitation Tolkien, there is plenty of influence from real medieval literature--the King Arthur stories (especially in the late fifteenth-century version by Mallory), the epics and romances, and the Norse sagas. The wizards and dragons now found in many works of fantasy were, however, very scarce in medieval literature, as I discuss more here. Interestingly, Tolkien never used King Arthur stories, having an expressed dislike for anything Celtic.
Another source is real medieval social and political history. George RR Martin says that he started with the history of the fifteenth-century War of the Roses in England (between the Yorks and the Lancasters, sound familiar?), though he kept the war technology in the pre-gunpowder era. And his very realistic and gritty touches, like food production and the horrors of war, are to some extent undercut by the dragons and walking dead--which are fantasy!
Another source is nineteenth-century folk tales. Not until the nineteenth century did people find it worthwhile to "collect" the stories told by the folk. These include everything from the stories of the Brothers Grimm in Germany to the Kalevala in Finland. There are some great stories here, and most have a sort of medieval-like look, but they aren't actually medieval--unless you assume, which I sometimes do myself, that in many ways life for three-quarters of Europe's population didn't really change from the Middle Ages until the coming of railroads and telegraphs and semi-universal education.