Thomas Aquinas, on whether the ends justify the means:
On the contrary, Boethius says (De Differ. Topic. ii) that "if the end is good, the thing is good, and if the end be evil, the thing also is evil."
That is a simple way of stating it, but he elaborates:
Nothing hinders an action that is good in one of the way mentioned above, from lacking goodness in another way. And thus it may happen that an action which is good in its species or in its circumstances is ordained to an evil end, or vice versa. However, an action is not good simply, unless it is good in all those ways: since "evil results from any single defect, but good from the complete cause," as Dionysius says (Div. Nom. iv).
Thomas is basically saying that, while there may be some good effects resulting from some bad action, the action (the means) must be taken into account when judging whether an end is truly good or desirable. We cannot justify an evil act through a good outcome—the act is still evil. Therefore if we truly wish to have a 'good' outcome, we cannot allow it to happen through an evil (immoral) method.
Aquinas speaks more eloquently about causes, species and accidents (which take a bit to wrap your mind around) in his article "Whether a human action is good or evil from its end?"