A herbivore is a creature that (when tamed) prefers to eat vegetable matter such as fruit, vegetables, fungi, and other plants. Apples and carrots are considered treats. They will seek out and eat safe vegan corpses such as lichens, mold, shriekers, blobs, and jellies. They will also eat melons, oranges, pears, wolfsbane, garlic, kelp fronds, and so on, but only when hungry. This applies if they happen to be on it or you throw it to them. If they are starving ("confused from hunger"), they will seek out and eat some people food like food rations, pancakes, and lembas. They will never eat meat products, meaty corpses, eggs, or tins. For the full list, see Diet § Pets.
When a herbivorous pet is used as a steed, it won't eat vegetables other than apples and carrots while you ride it, even if hungry. It will only do so when starving (which divide maximum hit points by 4). So if you don't have any apples and carrot and can't heal your pet, you might want to dismount it to feed it some fruits when it's hungry. For non-Knight players, this will keep tameness unchanged (+1 for feeding and −1 for remounting it).
Herbivores can be difficult pets to keep fed, especially for Monks, who share a similar diet unless they decide to take the small alignment penalty for non-vegetarian food. You might want to chat to your pet regularly so you know when you can throw it food.
Players polymorphed into herbivores are perfectly capable of eating meat.