There's a lot of luck involved, and just persistence over a long period of time. But I figure you also just have to start
doing what you want to do. This is amazingly possible in the developer world in a way that's hard to imagine in any other industry.
I was a music major in university (and planning on an English lit or creative writing minor) -- then I took an intro compsci course halfway through, and had a bit of a shock; it was an entirely new thing for me, but I really had a knack for it, and enjoyed it as well. It was a bit late to change my major... but I just fit in as much compsci as I could, and also just starting building things outside of coursework. I managed to get a software dev job right out of college based on a bunch of interactive online drills I wrote for the music department, plus I had very solid communication skills and academic recognition.
The software dev job was doing custom dev on lots of different kinds of projects (that was good), and in my spare time I turned the interactive music stuff into a site that's still a side income stream. It's languishing somewhat these days, because 5 years ago I started as a developer at a UK-based startup (working remotely from France; long story) and gradually became the CTO -- mostly just from looking for the hardest, riskiest problems we had and chasing them down one at a time, and being easy to work with.
It's hard to abstract any of this to general advice -- maybe just that mastering anything requires a good deal of focus and just time. If you want to do technical work (huge range there) you need to be doing it -- making mistakes, finding your way to solutions (good ones and bad), paying close attention to what's hard and why. It's probably worthwhile building something other people use, and can look at, etc. -- plus (whatever your focus is), mastering the context of working with customers, colleagues, competitors is valuable. If you build and support something all on your own, you're forced to learn a lot about all that.