'Grub' has been used as slang to mean 'food' since the 1600s, so it has a long history. It has also meant a drudge, a dwarfish person, a dirty child and a hard-working student just to mention a few!
I imagine that, in the case of food, it is related to the verb form of the word which means 'to dig'. In the same way, we often say when food is served, "Dig in!"