We are by nature social creatures, and when we are young sometimes it's difficult to find other children to play or "practice" with. Just as our brains create dreams probably to function as training grounds for exploration of situations and emotions, our brains also dream up daydreams and imaginary friends to help us learn to socialize.
Talking to Yourself
Or perhaps it goes a little deeper than that. Perhaps the child with an imaginary friend needs said friend for companionship. In The God Delusion, Richard Dawkins reprinted the classic poem by A. A. Milne, "Binker", to illustrate the point of how children create and use imaginary friends to serve needs for companionship. The child in the poem plays with his imaginary friend, teaches him to talk, and shares sweets with him -- all the while realizing that grown-ups don't "get it".
This deep-seated need for companionship gets expressed in the embodiment of so-called spiritual things. Gods, Great Spirits, and other deities serve the basic social needs of some people. Once we see a god as nothing more than an imaginary friend, we realize the same power exists: to comfort, to commune. God fills the gaps in people's lives who need someone to talk to; to understand what they're feeling or explore questions. God relieves the tension we feel and supports our natural desires for companionship.
You See it Too
Not only that, but another layer gets added on when believers fellowship with each other. Suddenly, a very real person with the same imaginary friend becomes a companion, and subsequent stories can be shared that reinforce the feelings supplied by the deity. Where the imaginary entity fails (in the lack of physical embraces, for example), the other believers succeed. This is a powerful social reality that has direct, lasting consequences and effects in the lives of real people, and is not to be overlooked.
Even if you, reader, are a believer in a god (or perhaps an imaginary friend), I encourage you to think about the idea that your deity is nothing more than a concept you invented. Think about what that would entail, and note the similarities if that were the case. I think you'll find, probably initially to your horror, that the similarities are immense. Your God hates the things you hate, loves the people you love, and will understand you deeper than any other person. How is that any different than Binker?