In an entrepreneurship class a long time ago, my professor explained a theory in psychology that there are three main kinds of motivation, and for each person, one tends to be dominant. The three motivations are: nAff (need for affiliation), nPow (need for power), and nAch(need for achievement).
Let's look at these in terms of company loyalty. You'll stay with a company if your job fulfills, or promises to soon fulfill, your dominant need. So what would make you leave? Well, if your job no longer promises to fulfill that need.
nAff is about friendship and acquaintance; you like your co-workers and you feel secure in your relationships with them. A highly disruptive event, like layoffs or restructuring or switching to a totally different team with little contact with your original co-workers, or a series of such events, can greatly reduce your nAff fulfillment. Similarly, if other people you know are deciding to take jobs elsewhere, you'll consider leaving too. Think of it as chasing after your nAff.
nPow is partly about having power (over people), and partly just about the prestige of having power over people. As long as you believe that other people believe you're important, you can generally fulfill your nPow. Conversely, finding out that someone just got promoted into a spot right above you, or finding out your team isn't so important after all, or, for a manager, finding out that your company isn't destined to be the next Amazon.com (when you previously thought it was) can be a big blow to nPow fulfillment.
nAch is not about people thinking you're important: it's about actually being important, even if it's to a project only you think is important. To see the difference between nAch and nPow, think of it this way: people with high nAff or nPow can't be happy alone; high nAch people might be perfectly happy on a desert island, if that's the best place to accomplish what they've been trying to accomplish. (Of course, some kinds of achievements require people, eg. customers.) nAch fulfillment isn't hurt much by imminent failure or by people telling you your goals are useless; those just make you work harder. But being prevented from achieving your goal, or finding out that the goal you've worked at for years is not useful after all, or finding out that your goal could be achieved with or without you - these cause serious nAch problems. And this can look confusing: even when you're highly productive at your current job, if you realize that someone else could do it sufficiently well, you feel as if you aren't achieving your potential. I know at least one person who left NITI because of this. High-nAch people are very hard to retain for long, particularly if you don't have an endless stream of serious problems to solve.
Incidentally, most geeks are nAch-dominant. Most managers are nPow-dominant. And most humans are nAff-dominant. Every company needs all three, although salaries do tend to be affected by the laws of supply and demand.
Look at the people around you. It's actually very easy to see which people have which primary need; everything they do reflects it. In a few cases, you can also see an unusually strong secondary need.
While we're applying "needs theory" to employee loyalty, you might be tempted to add a fourth need: nPAY, the definition of which you can probably guess.
In fact, the need to get paid is just a proxy when your job is not really satisfying any of your other needs. If you hate your co-workers, feel helpless to change them, and think your job is a waste of time, you might still do it if they pay you enough. That's because you feel that the money allows you to fulfill your primary needs outside work. For example, having a steady job allows you to hold onto a family, having lots of money makes you feel important when you buy stuff from a lowly salesguy, and being financially stable means you have the resources to accomplish things that might take money as well as effort.
Interestingly, from this point of view, money isn't just a proxy for goods and services; it's a proxy for satisfaction in any of three different forms. Who says money can't buy happiness? If your job is totally unsatisfying in the other three ways, you'd better hope it can!