First off, you need to get the three key ship upgrades you get via talking to your team-members: Armour (Jacob), Main Gun (Garrus), Shielding (Tali). One team-member will die for each of these upgrades not installed (IIRC it's one during the Oculus attack (if no armour), one during the passage through the debris field (if no shield), & one during the battle with the Collector ship (if no gun)). I think you should also get the unique tech/weapon upgrades (Mordin's Omni-Tool, Jack's Biotic Amp, Grunt's Shotgun, Legion's Sniper Rifle) some team-members have as this
may affect their survival chances if you leave them to "Hold.The.Line" at the end, although I'm not sure. Regardless, it doesn't cost much to buy/research every possible team-member upgrade, whether personal or ship. EDIT: Actually, as v0id points out below, Jack's upgrade is probably essential if you choose to use her for the Biotic Shield.
Second, you need to, at a minimum, make sure that
everyone used as a Specialist is Loyal
and is correct for the job, and make sure that your squad for the final section (boss section) are all Loyal. I think it is actually possible to keep everybody alive with only four Loyal team-members*. Although it's better & easier to have everybody be Loyal.
If a Specialist is wrong for the job, or is not Loyal, then they (or someone else depending on the section) will die.
If anyone in your final squad is not Loyal, they will die at the end after you kill the boss.
Specialist choices:
The Vents (Tech Specialist) - This
must be either a Loyal Tali or a Loyal Legion. A non-loyal Tali/Legion will die. Anyone other than a Loyal Tali/Legion will die.
2nd Fireteam Leader during the vent section - This
must be a Loyal Garrus/Miranda/Jacob. A non-loyal Garrus/Miranda/Jacob will cause the Tech Specialist in the vents to die (even if a Loyal Tali/Legion). Anyone other than a Loyal Garrus/Miranda/Jacob will cause the Tech Specialist in the vents to die (even if a Loyal Tali/Legion).
Biotic Shield (Biotic Specialist) - This
must be either a Loyal Jack or a Loyal Samara/Morinth. A non-loyal Jack/Samara/Morinth will cause one of your squad to be taken by the seeker swarms near the end. Anyone other than a Loyal Jack/Samara/Morinth will cause one of your squad to be taken by the seeker swarms near the end.
2nd Fireteam Leader during the Biotic Shield section - This
must be a Loyal Garrus/Miranda/Jacob. A non-loyal Garrus/Miranda/Jacob will die. Anyone other than a Loyal Garrus/Miranda/Jacob will die.
Crew Escort - Any Loyal team-member. A non-Loyal Escort will die. However, it is best to use a Loyal "squishy" character for Escort duty, such as Mordin, Tali, or Jack, as they have a lower chance than other characters of surviving during the "Hold.The.Line" section (when everyone else defends the door while you face the boss). Mordin's the best choice, as he actually has the lowest "Hold.The.Line" survival score. One or both of the other squishies can be guaranteed surviving by using them in your final squad, although should still survive Holding The Line if you have your best defenders (Garrus, Grunt, Zaeed) Holding The Line.
Your final Squad - Must be Loyal for them to survive. Don't take Garrus/Grunt/Zaeed as they are best left at the door Holding The Line.
*[e.g. Loyal Tali in the Vents. Loyal Miranda to lead the 2nd Fireteam both times. Loyal Jack to make the Biotic Shield. Loyal Mordin for the Crew Escort. For the final battle, use Tali & Jack in your squad (both "squishy"), & let everyone else (including Loyal Miranda) Hold The Line. Your squad survives due to being Loyal. Mordin survives due to being a Loyal Escort. Miranda being Loyal has a higher chance of surviving Holding The Line. Garrus, Grunt, & Zaeed kick ass Holding The Line even though not Loyal. Others Holding The Line should still live. I think........]
Crew Survival:
To save the crew, you must go through the Omega 4 Relay immediately after the crew kidnap sequence (the so-called "Joker mission"). If you do any missions in between, the crew will die, starting with Kelly. You must also then choose to send someone to Escort the crew back during the suicide mission - Loyal Mordin is best, but any Loyal character will do.
Best order to do things near the end:
Do
all available Recruitment & Loyalty missions prior to doing the Derelict Reaper IFF mission. Also either do all N7 missions prior to the Derelict Reaper or save them until after the end of the game. Don't do any after the Derelict Reaper and before the end.
After the Derelict Reaper, immediately activate Legion & then talk to him, to obtain his Loyalty mission.
Go to the galaxy map & go off & do Legion's Loyalty mission.
The next time you go to the Galaxy Map, it will trigger the crew kidnap.
After the crew kidnap, head off to the Omega 4 Relay.
If you activate the Galaxy Map after the Derelict Reaper
without having any active missions in your Journal, it will trigger the crew kidnap.
It is possible to do two missions after the Derelict Reaper & before the crew kidnap, so long as both missions are in your Journal before hitting the Galaxy Map after the Derelict Reaper. So it is actually possible to do both Tali's Loyalty mission and Legion's Loyalty mission after the Derelict Reaper and before the crew kidnap, if they're both in your journal before hitting the Map post-Derelict Reaper... meaning you can take Legion on Tali's Loyalty mission, which is quite interesting