Make your self invisibly essential to the company. Do things or have skills that nobody else does. The sorts of niche items that the company needs, but not enough to hire a second person to do them.
But also be "invisible" — as in, higher-up bean counters won't know you do them, so they can't be outsourced or reassigned. But still remain known to your immediate managers, and one level above, that you're the person who takes care of some mission-critical task, and so will ensure you don't get cut.
It's hard to find that thing, and not every company has this particular weakness. Volunteering to help out with projects in other departments is one way to explore the company for these flaws.
As a broad illustration, think about all of the thousands and thousands of employees that financial services companies have shed over the last decade. Then remember that they're still eagerly hiring COBOL devs.