Sometimes the amount of Stars you earn in battle can seem a bit random. Let's clarify how it works.
You will always get at least one Star for winning a battle. However, whether you get two or three depends on how much health your team had when the battle was won. Usually, having two heroes alive will result in two Stars. Similarly, winning a battle with all three heroes surviving is a pretty good way to get three Stars. But this is not always the case.
It is possible to win a battle with just one hero remaining and still win three Stars, if that hero represents the largest portion of your team's healths. Likewise, you could get just one Star for a battle if your team health had fallen close to zero.
If you're trying to complete a quest that asks you to earn a certain amount of Stars on a battle level, keep an eye on your team's health. Finishing the battle in the range of 75% of your team's health should guarantee a Three-Star win. Similarly, a Two-Star win can be accomplished with 50% of your team's health remaining.