There are two good ways to create lasting job security:
(1.) Make Yourself Indispensable To Your Boss/Company
If you like your current job and want to stay there, then the best way to encourage this is to make your boss realize that, no matter what he's paying you in salary and benefits, he cannot afford NOT to have you around. You do this by immersing yourself in the company at all levels. Learn the business practices. Cultivate relationships with your coworkers and customers. Dive deep into the codebase and learn how things work better than anyone else. You will, after some time, become invaluable as a resource to the company; if they were to let you go they would lose an incalculable amount of knowledge and experience that they may not be able to survive.
Understand that this strategy relies heavily on your visibility and the corporate culture. You may be the only person who knows how half of the codebase works. If the person with the axe doesn't know that, it is no help to you unless your boss is willing to speak up for you (and has the power to affect those decisions either directly or indirectly). This works very well in small businesses; distinguish yourself in your job and if/when budget talks start, the boss will hear your name and immediately think "no, we can't get rid of him, he's one of my best people!". If you are more than a couple of levels removed in the supervisory chain from the person who makes hiring/firing decisions, it doesn't work so well; you have to be so good that your boss' boss' boss will have heard about it.
Also understand that this does not mean you should "write yourself into your job" as a programmer. A lot of coders think they can attain job security by writing obfuscated or unnecessarily-complex code only they can understand. This only works as long as the boss knows no different. As soon as someone is hired who takes one look at the morass and goes "there's a better way", you're on your ass.
(2.) Make yourself a hot commodity in the industry in general
Whether you like your current job or not, the first strategy is never going to be 100% foolproof. If pillars of the American economy such as Lockheed and GM can lay off tens of thousands of knowledge workers in a desperate attempt to stay solvent, you can find yourself out of work despite your own best efforts and those of your chain of command.
So, when strategy 1 doesn't work, or if you hate your job anyway, then strategy 2 is to make sure you never have to look very hard for new work. You do this by looking very good on paper, to score those elusive interviews, and also BE good, so you leave the interviewer with the impression that his company simply cannot let you slip out of their hands. So, learn your primary programing language like it was your mother tongue, and that language's runtime as if you lived there. Keep up with other languages too; if you're a .NET programmer, stay up-to-date on what's up with Java, and vice-versa. Learn some "general-purpose" scripting languages like Ruby and Python. Get functional with Haskell or Erlang (there may not be a great market for new Erlang devs, but in learning a functional "duck-typed" language you learn how to solve problems in an entirely new way, much of which can be applied to "imperative" languages like the C family).
Be careful not to over-specialize; technologies do "die". Being a .NET WinForms programmer is counting for less and less as WPF gains popularity. Being a Java Servlet programmer's great, but when your boss asks you to develop an Android mobile app ("Hey, it's still all Java, right?") you better at least have an idea of where to start.