It depends on the sentence..There isn't really a way to know how and when to use them. Although, gerund's sound more natural, using an infinitive can *sometimes* make you sound... "hoity".. They mean the same thing.
Here are some examples:
Gerund- Reading is my favorite hobby
Infinitive- To read is my favorite hobby
The gerund sounds 100% more natural
Gerund- I like eating
Infinitive- I like to eat
Both of these sound completely natural!
Some times it's only acceptable to use one or the other.
I want to eat -infinitive.
I want to eating- gerund. This is 100% incorrect. do not say this.
If you're using "want" then you'll use an infinitive! This also goes for "need" as another example.
Check out these two links, they list verbs that require a gerund and verbs that require infinitives :)