Verbs in English can be divided into gerunds and infinitives. The main verb in the sentence will determine which one to use. You have to memorize which verbs are followed by gerunds and which verbs are followed by infinitives.
Gerunds can be used after certain verbs, and prepositions of time and place. They can also replace the subject and object of a sentence.
I fancy buying a new dress.
She is good at baking cakes.
Reading books is good for you.
Infinitives can be used after certain verbs. They are commonly used to show purpose.
I want to change the world.
She ran away from home to escape her problems.