In the early 1960s, Mort Weisinger, the editor of the Superman line of comics (including Action, Adventure, Superman, Jimmy Olsen, Lois Lane, and World's Finest) began to allow his writers to script what were called "imaginary" adventures. Of course, all superhero comics are "imaginary", but these stories allowed the writers to break some of the rules that were required with such successful continuing characters. We could see what might happen if Lois Lane married Superman (or Lex Luthor) while the next issue could completely ignore that wedding, because it had been clearly labeled an imaginary story.
I suspect in some ways this was an outgrowth of the way DC's comics of the early 1960s often featured what I call a "puzzle" cover. The puzzle was "Why is this happening?" For example, suppose the cover showed Jimmy Olsen doing something particularly nasty to Superman, like the following:
The last one even offers a double puzzle since not only is Olsen betraying his friend, but Brainiac and Luthor, Superman's worst enemies, are pleading for his life.
Anyway, the point is that DC loved these puzzle covers and so apparently did their readers. But of course it became tougher and tougher for the writers to create situations that managed to not only create a great puzzle cover, but which ended with everybody unchanged essentially. Hence the imaginary stories.
The concept has proven irresistable. Marvel published hundreds of "What If" issues and DC has its "Elseworlds" line; both are clearly inspired by the "Imaginary" stories. Of course you can track imaginary stories back to dream stories; the difference is that you don't have the falling asleep and waking up scenes.
I believe (but I'm not sure) that the first "Imaginary" story billed as such was "The Death of Superman" in Superman (V1) #149, November 1961. This is one of the most famous Superman stories of all time because it does not cop out at the end; Superman dies and Supergirl (until then still unknown) must carry on her cousin's tradition. (Correction: As noted in the comments, the first imaginary story is "Mr and Mrs Clark (Superman) Kent" from Lois Lane #19, August 1960).
In the story, Luthor convinces the world and Superman that he's reformed by finding a cure for cancer, then ambushes his longtime opponent and kills him with Kryptonite. Supergirl apprehends him (much to his dismay), but Luthor thinks he can escape the death penalty because he knows how to expand the bottle city of Kandor, where the trial is taking place, to normal size. However the Kandorians demand justice and Luthor is sentenced to the ultimate penalty.
There were some terrific "Imaginary" stories; "The Story of Superman Red and Superman Blue" in Superman #162, "Jor-El II and Kal-El II" from Superman #166, and "Clark Kent's Brother" in Superman #175 were all excellent three-part tales that explored Superman's character in new ways that would not have been possible otherwise.
Unfortunately, the great stories did not come without a price. Some stories which created bad characterization would be undone by the explanation that "it was an imaginary story". For example in Superman #205 (April 1968), it was disclosed that a heretofore unknown villain named Black Zero had actually destroyed Krypton. Jor-El was wrong, the planet would not have exploded without Black Zero to restart the nuclear reaction.
This of course was horrific characterization for Jor-El, who was God the Father in the DC universe in those days and the story was kicked under the carpet. In a similar fashion, DC explained in Flash #167 that Barry Allen had not been hit by chemicals and lightning by accident in Showcase #4; rather he'd been blessed by a Guardian Angel. Exit Guardian Angel stage left.
For the most part the "Imaginary" stories were confined to Superman, but a couple crept over to Batman via the World's Finest title, so we saw what would happen if Bruce Wayne had been adopted by the Kents. In an influential pair of stories, DC tried an imaginary look at Superman and Batman's sons in World's Finest #154 and 157. Amusingly, forced to come up with a wife for Batman, Weissinger chose Kathy Kane aka Batwoman, who had been retired from Batman for about two years.
No DC stories of the Silver Age that did not prominently feature Superman were billed as imaginary at the time, although they have been retconned to that status.