We've all experienced the sensation of finally learning to do something after hours of failed practice. Somehow the brain seems to distinguish between successful and unsuccessful attempts. Aabha's question is a good one but the answer is very simple: nobody has even the remotest clue! What we do know is that memories are stored in different places around the brain and that when you reinforce a thought pattern, physical movement etc. your brain often becomes quite good at repeating it. But the brain is still flexible enough to allow you to try the skill (mental or physical) in different ways.
What's also interesting is that it doesn't happen instantly. When you learn to juggle, for instance, you don't suddenly find there's a moment when it clicks and you can juggle perfectly. You tend to find (I did at least) that once you start doing the juggle successfully, it still takes a couple of weeks of practice before you stop making the old mistakes. The same can be said of learning a fact in school, often it takes reinforcement before we stop making the previous mistakes.
Although, some types of information do seem to be like flipping a switch. If someone has always thought Mars was the colour green, it takes 30 seconds for them to learn it's red and they never make the mistake again.
All we can really say is that the brain has the ability to repeat its' activities, vary them, self-analyse, self-observe and compare actions with an internal set of "success criteria". Sometimes it can take a long time for the brain to learn the correct pattern, sometimes it can be instantaneous. Sometimes the brain rewards you for a new way of thinking, sometimes it punishes you (like when you lose sleep over a new frightening statistic you've learnt).
As a teacher, I wish I knew the secret to helping people learn something. If I knew (or if anyone knew) what was going on inside the brain when we learn a new skill, fact, way of thinking...I'd have discovered the silver bullet of education. Sadly our knowledge of the brain is nowhere near good enough to answer the question even a little bit. All I can say is that your brain can somehow distinguish correct from incorrect IF both options are made explicit i.e. you only know you've got it wrong if you know what right looks like.