The idea that comic book characters all occupied the same world was first introduced in 1940 by the comic All Star Comics #3. Before then, superheroes existed in their own realities and had their own adventures. At the time, DC as it's known now didn't exist yet, and its superheroes were spread out between All-American Publications and Detective Comics, Inc. (The two companies had the same CEO, though they operated as independent publishers. Both later merged into National Periodical Publications, which changed its name to DC—confusing, I know!)
Writer Gardner Fox, inspired by the National Baseball League, thought it would be neat if all All-American heroes came together to form a team. Thus, The Justice Society of America was born. The crew, featuring The Sandman, The Flash, Green Lantern, and more (Batman and Superman were still part of Detective Comics at the time), combine their powers to fight evil. Even so, it would be a long time before a solid continuity between the characters was expected by fans or created by the writers.