Sometime around 1260 a Danish Dominican named Augustine of Denmark condensed several centuries of patristic and monastic reading practice into four short Latin lines, a couplet still quoted in modern discussions of medieval exegesis: the letter teaches what happened, allegory what you should believe, the moral sense what you should do, and the anagogical sense where you are heading. The verse did not invent the four senses so much as give preachers and commentators a portable summary of a method already centuries old, traceable through Origen's allegorical reading and John Cassian's earlier threefold spiritual scheme, gradually fixed into this exact fourfold shape by twelfth century monastic and scholastic writers.
In practice a medieval commentator working through, say, the story of Jerusalem might explain the literal sense as the actual city in Judea, the allegorical sense as the Church, the moral sense as the individual believer's own soul, and the anagogical sense as the heavenly Jerusalem awaiting the blessed, four readings of one word held together rather than four competing guesses. Thomas Aquinas gave the scheme its clearest theological defense, insisting that only the literal sense could ground a theological argument, since the other three senses are always derived FROM the literal sense's account of things and events, never invented independently of it, which kept the method from collapsing into arbitrary allegory.
The Reformation's insistence on the literal sense alone, sola scriptura read narrowly, pushed the other three senses out of mainstream Protestant exegesis, but the scheme never disappeared from Catholic teaching: the Catechism of the Catholic Church still explains all four senses by name in its own account of how scripture is to be read.