When it comes to the use of gerunds and to-infinitives, there can't one simple rule, unfortunately. More often than not, you have to remember patterns. Let's take the noun "aim" as an example:
- Aim + of + to-inf: The aim of the project is to help the homeless. (this explains the use of "to give" in your sentence)
- with the aim + of: We visit schools with the aim of getting kids interested in the issue.
"How to do something" is also to be memorized. As a rule, "to" is followed by infinities but not always (e.g.I'm looking forward to hearing from you):
- Will you tell me how to do it?
Answering you question "How do I decide whether I use each of them?", I'd say use a dictionary when in doubt. It usually gives all the information on patterns. For example, the Collins Dictionary does it like this:
Take note of what is given in the brackets.