Once a human-created algorithm can program an algorithm better than a human can1, it can by definition create an algorithm better than itself2. At that point, everyone will be out of a job, not just programmers.
On the other hand, there’s certain to be a gradual process of automation before we reach the "technological singularity"3. The best way to hedge your bets if you want to remain gainfully employed a few decades from now is probably to develop your creativity as much as possible (that’s true for basically any industry). As a programmer, that’d mean honing skills like UX design, visual design, software architecture, information architecture, and similar.
1. Try saying that fast 10 times!
2. This is a simplification, of course, because perhaps the algorithm can only create (for example) obstacle avoidance algorithms, not algorithm-writing algorithms. But for the sake of argument, we’re talking about “general AI” rather than “specialized AI” here.
3. This is what many futurists call the point in time where technology starts improving exponentially due to general AI that can improve its own code.