They are pretty close in meaning, actually! Here are the rules:
to when you want to express that the result of the verb will affect or in some way be done /to/ the following noun.
for when you want to express that the action is being done for the sake of or on behalf of the following noun, or when the preceding object is intended to be given, to affect, or be caused or expected by the following noun.
These can overlap a lot, so sometimes either one is good!
What is more of a problem above is confusing
ensure to indicate a process of making an uncertain event more certain.
insure or when you mean taking a course of action that makes the negative effects of a contingency less costly or bad, or when you are speaking of an insurance (financial product, or other object which insures in the first sense).
We need to pollute less, because not doing so will cause harm to all of humanity.
^ This means that if we do not pollute less, all of humanity will be harmed as a result.
We need to ensure the minimum amount of harm to all of humanity.
^ This means that should do so so that all of humanity will be harmed minimally.
We need to ensure the minimum amount of harm for all of humanity.
^ This means that on behalf of, or for the sake of all of humanity, we need to make certain that we do the least harm. The object of the harm is not specified, but we understand it to also be
It could also mean roughly the same as the above example - because the
minimum amount of harm is intended
for all of humanity by the recommended action.
If you were to say:
We need to pollute less, because not doing so will cause harm for all of humanity.
^ This would not be incorrect grammar, but it is less common use, and may sound awkward. Although most listeners would understand what you meant, it can sound like
all of humanity wants the speaker to cause harm to some unspecified thing, and the speaker does not want to fulfill their wish.
It could also have the second sense of
for above, so it isn't "wrong" and you may find some occurrences where
for is used like this. But you should likely prefer to use
to here instead.