It's a big question and not one I claim to have solved, but since you ask I'll go through things I think about :) I'll say up front it's a huge, ongoing challenge that I keep trying to improve on.
But to get to the question...
One thing to get out of the way: get money off the motivation table. It almost never works as a positive, but it can easily be a negative. Pay people well enough that they don't need to worry about it.
After that you have a few things to consider:
Are people doing interesting work? Everyone has to do their share of the daily grind, but do you have anyone on your teams who's doing nothing but the grind; or nothing but woolly R&D that never ships? Both are demotivating.
Are people doing a fair workload? Pretty simple. Make sure everyone's working to a comfortable, sustainable maximum. I find smart people are happy when they're doing what they do and doing it really well.
Do people have real input into how things are done? Hiring smart people then ignoring them is disastrous. Giving them room to move can be amazing.
How do people produce their best work? What are you doing to ensure they are in that mode as much as possible? Are they self-aware about getting themselves in their zone?
What frustrations do they have and how can you reduce those frustrations? Some things you can fix. Some things you need to escalate. Some things you coach on how to deal with it themselves.
Is the work and environment compatible with their personal values? It can be really simple, like making sure a family-motivated person can leave on time to get home to their kids.
What are peoples long term goals? Are you helping them get there? Have a specific learning plan, and give people opportunities to learn the way that works best for them.
A lot of this relies on having a weekly 1:1 meeting. I have one scheduled every week with every staff member; and I avoid rescheduling them if at all possible. I have a rolling set of topics including "what haven't we talked about that we should?" (a question a previous boss once asked me and I shamelessly stole :)).
Worth saying that you can do all this and other factors can still completely demotivate people. We don't control the world. Maybe one last thought is - if someone seems off their game, if something doesn't feel right, ask them what's going on. You can't help anything if you don't notice or care enough to ask.
TALK TO THEM. LISTEN TO THEIR PROBLEMS. HELP THEM GET THE JOB DONE.
... and if they have a complaint, don't just dismiss it! Don't micromanage and dictate HOW they get things done; but be there if they need it!
But more importantly, HAVE THEIR BACKS! There's a difference between a "boss" and a "leader". A leader is there for the TEAM. You are there FOR THEM, not the other way around. If you aren't there FOR THEM then everything falls apart.
To borrow from Patton:
There is a great deal of talk about loyalty from the bottom to the top. Loyalty from the top down is even more necessary and is much less prevalent. One of the most frequently noted characteristics of great men who have remained great is loyalty to their subordinates.
That's not some feel-good bull you slap on a bumper sticker. In that way office-space relations have become increasingly less reliant on actual interpersonal communication and depending more and more on automation that makes things LOOK more productive, but the end result is more often than not little more than shantytown sausages filled with mystery meat.
You can see this with things like version control software. As I often say back in the day on major projects we had project managers who did their huffing job riding herd on the line coders. Holding meetings not to bark marching orders or promote themselves, or to randomly change direction over something they read in Forbes, but to let everyone have their say and contribute to the whole! To iron over problems, to create an environment in which people can voice their problems and EVERYONE else contributes to help remedy things.
Nowadays it seems like most "project managers" aren't worthy of the name, trying to let GIT do their job whilst locking themselves in the office to play Facebook games all day -- and they'll throw ANYONE who jeopardizes their position under the bus as an excuse to stay exactly where they are. That's why fresh out of college entry level programmers have an employment term roughly equal to the life expectancy of a mayfly! Rather than building up an employee for life with upward mobility, they're treated like toilet tissue -- wipe a few times, then flush when a new class of eager faces is ready to be exploited.
In that way, another tidbit of wisdom from GSP Jr works well:
Do everything you ask of those you command.
A good leader doesn't sit back behind the lines -- he's down there in the trenches doing the work alongside his men. ANYTHING else is NOT leadership!!!
They see you in there, fighting the same fights and struggles as they are, promoting the team and trying to solve problems, THEN they will accept you as a leader AND the project should run smoothly. THEN they will not be afraid to come to you when they have a problem, or try to cover it up as they know you will be there to pick them up and dust them off.
FAR too many office environments right now are more about "cover your ass and screw everyone else" -- and that's NOT a healthy environment in which to build a project. YOU MUST BUILD TRUST!
... and to be frank, in my experience trust is a commodity that seems to have all but evaporated from development the past decade and a half; and it gets worse EVERY damned year!