Think of almost anyone in the book and Gerasim will turn out to be a foil for that person. He's the only character who is both willing to be open and honest with Ivan about the fact that he's dying, and who isn't horribly troubled by the fact of death. He's also the only one who shows Ivan genuine sympathy, and is actually willing to care for him. Everyone else – Praskovya Fedorovna, Lisa, Ivan's friends, the doctors – tries to keep up the charade that Ivan isn't dying, because they themselves are too afraid of death to deal with it out in the open.
The other characters also show Ivan very little genuine sympathy. Praskovya Fedorovna's appears to do so on occasion, but almost all of her attempts at comfort seem fake. And whereas Praskovya Fedorovna and the doctors look at Gerasim's treatment of Ivan – holding up his legs – as ridiculous and somewhat improper, it's the only thing that's actually capable of comforting Ivan. In fact, Gerasim's approach to both death and life is meant to contrast explicitly with everyone else's. (And there is a bit of a class thing going on here: Gerasim's a peasant, the rest are middle class. This might shows where Tolstoy's sympathies lie).
Of course, up until the end, Ivan himself is much more like his friends and family than he is like Gerasim. He's serves just as much as a foil to Gerasim as they do.