“The ends justify the means” is not a direct quote from Machiavelli's The Prince (or from any of his works, really), but rather a broad generalization of his ideas presented through the book. The closest Machiavelli ever comes to saying this outright is in Chapter XVIII, where he says that “we must always look to the end” especially when princes are concerned (49). “The ends justifies the means” sounds like a call-to-arms to do whatever is necessary to reach a goal, and, of course, like a recipe for general evil. The idea appears to be too cruel to defend for people who have an ounce of morality in them.
In regards to ends and means, Machiavelli does not ignore the moral implications of this idea; for instance, he states that the prince should always be “ready to enter on evil” (49), but only if he must – that is, if it is for the good of his country, not for his benefit. At one point, Machiavelli states that a prince should moderate his actions with “prudence and humanity” (46). This is why I think “the ends justify the means” should not be approached as a free-for-all, morally corrupt method, but a spectrum. He gives two examples that explain this point. His first example is Agathocles, who overtook a city state by force (through deception and by murdering the city's senators and rich people). The city had been under siege by another city for some time. After seizing the power, Agathocles successfully defended the city and repelled the attackers, and the city prospered after that. Machiavelli actually criticizes Agathocles' actions for their “fearful cruelty and inhumanity”, claiming that he has no place among “the really excellent men” (25). Regardless, Agathocles' actions are considered justified and much better than Machiavelli's second example, as they led to a much better result for the state. Oliverotto de Fermo, Machiavelli's contemporary, seized the city of Fermo through similar means, but massacres continued even after he took control. He would execute people for little purpose and terrify the citizens with the help of his military in order to stay in power. In this case, the ends do not quite justify the means, because Oliverotto struggled to remain in power at the risk of the citizens, bringing harm to the “entire community” (45) and therefore going against Machiavelli's idea that cruelty is only justified if it is for the purpose of keeping citizens in peace, united and prospering. In other words, Machiavelli promotes the idea of properly used cruelty; there is no right or wrong for a ruler, but good and bad ways to be cruel. The distinction of “good” and “bad” is based on whether the means lead to a benefiting end for the whole state. For a more contemporary example, Hitler would be an awful leader by Machiavelli's standards (very obvious moral problems aside); not only did the Holocaust not benefit the state and average citizens whatsoever, the economic boost post-Hitler was pretty short-lived as it fed from the war and, while Germany is a prosperous nation today, it still carries a stigma. There are examples where we have morally good means reaching a morally good end, like Martin Luther King and the civil rights movement in the US, which was non-violent and earned African Americans their civil rights. “The ends justify the means” is, therefore, not a concrete rule but a spectrum. Particular means may justify particular ends, and Machiavelli's only standard is that the means bring justifiable benefits to the state as a whole.
So, in general, “the ends justify the means” is not really an absolute statement and not as devoid of compassion and morality as it initially seems. A completely corrupted leader with no sense of morality may use this idea as an excuse, as Machiavelli is solely concerned with the state (his state Florence in the book, to be exact). His idea may excuse conquering another nation if it is for the good of one's country and people, for example, but if this action has a negative impact (economically or socially – such as by causing unrest and a growing dislike for the ruler) then it is not really justifiable. It is also worth noting that Machiavelli writes explicitly for rulers, not common people – he doesn't comment on whether cheating in an exam is justified if you're going to get a good grade, or stealing is okay if you're hungry.