How does the brain create original thoughts?
To understand how the brain "knows" what to do, we can look at how children learn language.

At first, children randomly create all kinds of sounds, including those that aren't part of the language(s) spoken in their environment. Parents and other people react to these sounds, repeating them, speaking their language, smiling, etc., and the children in turn react to these reactions to their sounds, learning which sound elicit which reactions (e.g. which sounds lead to a smile, which sounds are ignored, etc.), until they slowly begin to build a repertoire of sound and "pre-words" which give the most pleasurable results.

With time, children learn to recognize parts of the language spoken in their environment and begin to emulate these sounds, learning their first words, and eventually to speak.

So what we have here is a brain creating random behavior (in this case with the speaking apparatus), and learning from the effects this behavior elicits in the environment which behavior leads to the desired results. Whatever happens in the brain when a desired result is achieved, is stored as the "internal behavior of the brain" to do to achive that result, e.g. what to say to get that ball.