Practice.. A lot
I have had this single project that I have been working on for about 2 years. I have started over plenty of times to learn from my past mistakes.
Don't be afraid to sit and plan your code befor you start, that is the main thing. Do a needs analysis on what you are doing and find out what kind of implications certain actions will have on your code down the line.
Then when you have your project just about done.. Lose intrest for about 3 months....
When you come back to it it will be pretty impossible to remeber what the heck you were doing.
Then start over with that same goal in mind. Somthing that I couldn't even figure out for 2 years, I have taken further in 2 days than I could have though back then. I turned somthing like 2000 lines of code into a very efficent 500 lines of code project. And the way that I planed it all my libraries are portable enought to be moved from project to project for anything I do in the future.

Google.... That is my final answer.. Google any problems you have because chances are people have had the same problem, and if you cant find your answer then you truly dont understand the problem because you are probably not asking the right question.

Good luck