The instances where we use the gerund after "to" seem mainly to arise when the "to" is part of a phrase ending with it, after which a noun would be required. This is the case with your example as well: it's "the trick to [something]" because the "to" really applies to the "trick". Since the "to" applies to the trick, and we need a noun here, we use the gerund. Similarly, we could say "The trick to the game is..." because "the game" is a noun.
Other examples might include:
"We had to resort to using force." ("to resort to [something", we could say as well "We had to resort to our backup plan.")
"The key to winning is to stick in the corner." ("key to [something", we could say as well "This is a key to the door.")