The libretto of Tannhäuser combines mythological elements characteristic of German Romantische Oper (Romantic opera) and the medieval setting typical of many French Grand Operas. Wagner brings these two together by constructing a plot involving the 14th century Minnesingers and the myth of Venus and her subterranean realm of Venusberg. Both the historical and the mythological are united in Tannhäuser's personality; although he is a historical poet composer, little is known about him other than myths that surround him. Furthermore, half of the opera takes place in a historical setting, and half takes place in the mythological Venusberg.
Wagner wove a variety of sources into the opera narrative. According to his autobiography, he was inspired by finding the story in "a Volksbuch (popular book) about the Venusberg", which he claimed "fell into his hands", although he admits knowing of the story from the Phantasus of Ludwig Tieck and E. T. A. Hoffmann's story, Der Kampf der Sänger (The Singer's Contest). Tieck's tale, which names the hero "Tannenhäuser", tells of the minnesinger-knight's amorous adventures in the Venusberg, his travels to Rome as a Pilgrim, and his repudiation by the pope. To this Wagner added material from Hoffmann's story, from Serapions-Brüder (1819), describing a song contest at the Wartburg castle, a castle which featured prominently in Thuringian history. Heinrich Heine had provided Wagner with the inspiration for Der fliegende Holländer and Wagner again drew on Heine for Tannhäuser. In Heine's sardonic essay Elementargeister (Elemental spirits), there appears a poem about Tannhäuser and the lure of the grotto of Venus, published in 1837 in the third volume of Der Salon. Other possible sources include Friedrich de la Motte Fouqué's play Der Sängerkrieg auf der Wartburg and Eichendorff's Das Marmorbild (The Marble Statue, 1819).
The legend of Tannhäuser, the amorous crusading Franconian knight, and that of the song contest on the Wartburg (which did not involve Tannhäuser, but the semi-mythical minnesinger Heinrich von Ofterdingen), came from quite separate traditions. Ludwig Bechstein wove together the two legends in the first volume of his collection of Thuringian legends, Der Sagenschatz und die Sagenkreise des Thüringerlandes (A treasury of the tales of Thuringian legends and legend cycles, 1835), which was probably the Volksbuch to which Wagner refers to in his autobiography. Wagner also knew of the work of another contemporary, Christian Theodor Ludwig Lucas, whose Über den Krieg von Wartburg of 1838 also conflated the two legends. This confusion (which explains why Tannhäuser is referred to as 'Heinrich' in the opera) does not fit with the historical timeline of the events in the opera, since the Singer's Contest involving von Ofterdingen is said to have taken place around 1207, while Tannhäuser's poetry appeared much later (1245–1265). The sources used by Wagner therefore reflected a nineteenth century romantic view of the medieval period, with concerns about artistic freedom and the constraints of organised religion typical of the period of Romanticism.
During Wagner's first stay in Paris (1839–1842) he read a paper by Ludwig Lucas on the Sängerkrieg which sparked his imagination, and encouraged him to return to Germany, which he reached on 7 April 1842. Having crossed the Rhine, the Wagners drove towards Thuringia, and saw the early rays of sun striking the Wartburg; Wagner immediately began to sketch the scenery that would become the stage sets. Wagner wrote the prose draft of Tannhäuser between June and July 1842 and the libretto in April 1843.
Wagner began composing the music during a vacation in Teplitz in the summer of 1843 and completed the full score on 13 April 1845; the opera's famous overture, often played separately as a concert piece, was written last. While composing the music for the Venusberg grotto, Wagner grew so impassioned that he made himself ill; in his autobiography, he wrote, "With much pain and toil I sketched the first outlines of my music for the Venusberg.... Meanwhile I was very much troubled by excitability and rushes of blood to the brain. I imagined I was ill and lay for whole days in bed...." The instrumentation also shows signs of borrowing from French operatic style. The score includes parts for on-stage brass; however, rather than using French brass instruments, Wagner uses twelve German waldhorns. Wagner also makes use of the harp, another commonplace of French opera.