Three reasons cover most of it: entertainment, recreation, and education. Early electronic games were partly experiments to show what computers could do, and partly a way to give people something fun to do with them.
That mix never went away. Today the same medium produces pure escapism, competitive sport, and classroom tools that teach maths, history, or medicine, all built on the original idea that a machine can be a toy.