I am a not so social software engineer, so take it with a grain of salt. I think I would divide it into three categories

* Perks and incentives
These are things like coffee, having an espresso machine at work, getting access to free snacks. Gym access is another one I have commonly seen. I have also seen things like programs that give you access to free classes or certification programs (when the hell am I supposed to have time to do that?) These little things can make my day better and lighten my mood when things are going well at a company.

* Caring about you as a person (?)
I would call this roaming the halls - or when management walks around asking people how they are, what did they do over the weekend, are they okay. This can be a great way to gauge what's going on in a small to medium business. That is until it's the same question day in and day out, or you're explaining your hobby to the same person for the fifth time and they obviously aren't listening, then it feels forced and the illusion fades. That said, I'd rather have the person that makes the effort as a manager to get to know me at a least a bit, than to have a suit treat me like a number on a piece of paper. This is another one where when things are going well at a company, I think that management showing they care is important, and it does make me feel more motivated to do my job.

* Gatherings
I call these "Mandatory fun", I have never been one to want to go hang out at a bar with coworkers after work. It feels forced and awkward to me. When you're spending the majority of your week pulling your hair out trying to get a product working, or bugs fixed, or put out fires, or come back from a layoff - it's hard for me to find these to be fun. That said, I know a lot of other people do find them fun, so I understand why they happen. But using a pizza party to try to cheer up employees instead of communicating what is going on at the company is a half measure that just erodes trust over time.

Where this all falls apart for me is when a company starts to have issues. I have personally found that when you don't have transparency in a company, you lose trust. Is my boss being nice to me because they really don't want me to quit? Or because they're a nice person? Staying motivated when you're facing impending failure is what made me want to respond to this. When everything is going well, it's easy to stay motivated, when things get rough, everything changes.

The perks start to vanish as budgets get cut, the lazy conversations about how much fun you had over the weekend are replaced with monosyllabic responses - you don't have time because you have to ship that feature yesterday. You start to realize that you're dreading going to work. You start to realize you don't know what you're doing anymore. You start you realize this isn't what you thought it would be.

How do you stay motivated during that?

The only thing I have found is sheer willpower, fighting through it and just telling yourself to keep going. Carefully managing what you can and can't do so you don't burn out. I have yet to have a company offer anything that really motivated me during those times other than clear and honest explanations of where we are, what we need to do, why we need to do that, and how that will make my life better.

Transparency and honesty has been the only real motivator for me when things are tough - but the coffee is nice too.