To have a canon, you need the authority to create one. For Star Trek, the canon began when Gene Roddenberry and the other original Star Trek creators put together the Writer’s Guide (I’m looking at its Third Revision from April 17, 1967 right now), which is also commonly--and significantly—called “the Bible.”
It describes the setting (including details of the Enterprise, etc.) and the characters (including ages, backgrounds, etc.) I presume this is done for most TV series. But because Star Trek was at that point unique—a prime time dramatic series with a science fiction premise—the Guide stresses the principle of believability. Captain Kirk has to be believable as the captain of a ship, even if that ship is in outer space.
Believability was essential, and the principle extended to two other important features of Star Trek: plausibility (a plausible even if not yet possible scientific basis for each phenomenon and piece of technology) and consistency: what is established as true in one episode (the ship’s drives, how many settings the phaser has) is true in subsequent stories.
That consistency is partly where canon comes from, and also one of its most important effects: consistency creates a universe, a ground of reality.
There’s another word that’s used to describe this consistency over time: continuity. In filmmaking, continuity usually operates within a single movie, and may mean something as simple as the guy who goes into the office building wearing a red and black tie should be seen getting into the elevator wearing that same red and black tie. But because movies are shot out of sequence (six months may have passed between shooting those two scenes), keeping track of the continuity of such details became the responsibility of a person at first called “Script Girl,” then “Continuity,” and now “Script Supervisor.”
But in the Trek canon, continuity spans episodes and even centuries. It took awhile to establish “Starfleet” and “the Federation,” but once settled, every movie and every episode of every series afterwards adhered to their basic descriptions. Then as more names were added (of people, ships, places, events) and their functions in time dramatized or talked about, the canon universe grew over space and time.