"World Wide Words", says:
The source is the old Germanic word meaning to dig (which is also the source of grave). The verb to grub came first in English, around 1300, and meant just what it still does: to break up the surface of the ground or to clear the ground of roots and stumps. Derived from it is our adjective grubby for somebody or something that is dirty and the Australian term grub for a person who is unclean or who has messy habits.
The connection with food is the idea of animals foraging for food. In their wild state, for example, pigs grub for edible roots and the like. The larval sense comes from this, because grubs often feed in leaf litter or around roots. The slang sense of human food appears around the middle of the seventeenth century and is also linked to grubbing in the ground for something to eat.
Interestingly, in case you should think that slang words must either soon transform themselves into standard English or quickly vanish again, grub has remained slangy — at best informal — in all the years since, as have compounds like grubstake (from the nineteenth-century goldfields practice of investing in a prospector by providing him with money to buy food).