Games developers usually know exactly what they're doing. They approach a project with a specific vision, and the majority of the time, the finished article doesn't deviate too far from the sketches on the drawing board.
On other occasions, they encounter budgetary issues and creative roadblocks during the development process and are forced to make concessions, in some cases bigger than others, and this can completely reshape the game in question.
These cases are well documented, but there are rare instances where games have been created entirely by accident, in a way that's almost comparable to Sir Alexander Fleming's discovery of penicillin among his mouldy petri dishes.
There are cases where studio employees have had so much fun with bespoke non-gaming software that they've slapped it with a few bells and whistles and released it for public consumption. Other games started out in a different form until a glitch saw them mutate into something better, like Bruce Banner and the gamma bomb.
Games of all shapes and sizes have bizarre origin stories like these, from indie titles, to one of the biggest franchises of all time. These are the happy accidents that have shaped the industry as we know it.