No one is irreplaceable. But replacing people is hard, expensive and takes time.
Motivation is the psychological cause of an action.
If you want to predict how people will act, you must know their motivators and demotivators.
Internal vs External Motivation
There is internal motivation, doing an action because of the result, and external motivation, doing an action because of something unrelated e.g. money.
You should focus on recruiting employees that have high internal motivation in what your company does or what is their role will be in the company. Additional external motivation helps preventing burnouts and keeping your top employees, but internal motivation cannot be forced.
Even highly motivated employees will leave any company if they encounter stupidities. You are then left with only second grade employees.
Second grade employees avoid leaving and complaining because they fear they cannot get a job elsewhere. They have an external motivation of fear. This is especially the case with software developers.
This causes second grade employees to stay in a single company for longer and getting more prestigious role. This can lead an organization becoming less smart in stages, getting more decaying and ineffective when you go higher in the organization.
Bad employees usually want roles that are some form of maintenance which makes them even harder to replace. And they surely will not document any processes if management does not tell them to.
Acknowledge the result. For example, getting an award if you get a task or feature implemented ahead of schedule. Value of the award does not usually matter, only the recognition. Surprise your team with a treat after stressful goal. Programmers will write better code if they know the code will be inspected. It does not have to be a formal inspection, just the knowledge that someone will read it.
You feel more attached to IKEA furniture because you build them.
Luxury. Money is not a good motivator after good base salary but luxuries are. You can offer breakfasts, lunches, dinner, sodas, candies, haircuts, massages and gym memberships. Money and all kind of bonuses can motivate temporarily but help less in the long run.
Pay interest in personal development. Assigning mentors. You do not need to state that person X is person Y's mentor. Make them work together if you see that they share common interests but X has superior skill in it. Guide people to paths for personal advancement e.g. giving one person challenging tasks in your new NoSQL database so he can become NoSQL expert.
Give responsibilities. Should be monitored by the project lead of course but now he has a chance to really show what he can do. But avoid creating a situation for one-man problem where e.g. you have only one person who knows how your API authentication works.
You are now responsible for the security of a upcoming product.
Allow people to make more decisions. Providing explicit choices among alternatives can enhance motivation to selected alternative. State that if current project goes well, the development team can suggest a programming language and framework for their next assignment. If people are allowed to select the tasks they enjoy, their motivation to that task and unrelated tasks increases. Having too many choices may lead to increased anxiety.
Let people change focus inside the company. Allow them to find their passion or do something else for a change. Usually working in the same role and team for two years makes person want a change. Small hacks, new projects, learning new stuff or give them totally new job.
Reduce overhead required to do stuff. Less formal face-to-face code reviews, less formal design reviews, less meetings, less directives from up high. Automate away the tedious.
Get rid of bad employees. Team members inspire each other. People should rewarded for the group performance, not individual performance. Professionals like to work with other professionals or people willing to learn. Bad employees are toxic and demotivate the whole team.
Have extra-long breaks. 1 hour lunch, 15min breaks here and there.
Encourage socializing. Especially on breaks, lunch and after work. A laugh goes long way.
Reduce number of parallel tasks that a team is working on. Makes them focus on single thing at a time so the work load does not seem overwhelming.
Motivation from cause. Employee may be motivated by the goal and values of the company. Each employee should be at least partially motivated by this.
Being loyal is a two-way relationship. If people burnout or have problems at home, give them a vacation. Likewise, if company hits a bad patch, people should still stick with the company.
Work environment should be good. You should have a nap/resting room and a coffee room. Each employee should have a working space that does not disturb his work flow.
Work should be challenging. Tasks that are too difficult raise anxiety. Tasks that are too easy raise boredom. Most important to this is to listen and act based on feedback.
How to Demotivate People
Futile work. Perception that the current project is futile or of minimal value. People do not like to do futile work, even if they get paid. If a long running project fails, somehow acknowledge the teams work in it. Let them show how it worked before it is cancelled, reuse components in other projects etc.
No obvious path of advancement. Lack of mentor who can make them better at their job. Can be either personal or professional advancement. Giving feedback is part of the leader title and should be done monthly.
Promotions a matter of time rather than merit. Demotivation comes from the basic unfairness of it. Not-so-smart co-worker you have might get a promotion because he has been in the company for all his life. Might seem like a good idea to boost loyalty but do you really want loyal second class employees?
Lack of freedom. If your work is constantly being monitored and controlled, you do not enjoy working as much. Management should not micromanage. No possibility to get involved in decisions making.
Processes that get in the way of development. Unnecessary processes create frustration with the organization as a whole. You need a large reformation which is usually impossible.
Conflicting values. Conflicting values between co-workers can cause demotivation. This can be fixed by recruiting people according to company values, thus all people should share more or less equal personal values.
Lack of resources. Poorly invested equipment is common in bigger e.g. if programmer has to work with a computer from 2007 or an artist cannot get a drawing board.
Bad project management. Too much work to do at once. Lack of focus when assigning the tasks.
Leeches. Do not tolerate employees that do not achieve results because they are lazy. A single leeching employee can demotivate a whole team. You need to be a team, not a family. Even nice people can be leeches. Detect people that seem to work but get no results, effort is not enough. People that do not seem to work but get results are ok if other employees are fine with it.
Jerks. Do not tolerate people that hurt the feelings of other people. Do not tolerate jerks even if they get great results. No one is productive when working with a jerk. Jerks make your company seem like a jerk company. Rude employees cost businesses.