The gerund is quite simply a verb turned into a noun. If you could use a common noun (or proper noun, or pronoun) after “to”, then you can use the gerund (-ing form). If you’re sure you need a verb, then use the infinitive. In some cases where we can use a noun, an infinitive would also work (but make sure the meaning is the same; it might not be). In such a case “to” would mean “in order to”.
''That is the key to *the castle.*
''You are now on the path to *the castle.*
''There are 5 steps to *the castle.* (This would work either way, although I prefer the gerund.)
''She might not have the skills to… (“The castle” does not work here. We need verb. The meaning is “She might not have the skills that she needs so that she can….)