Pets cannot be fed in combat. You can feed a pet in the same circumstances that you can eat.
Having to keep food for your pet requires you to use up slots in your bags for food, limiting what you can carry. Having a pet that can eat food you can harvest while leveling mitigates this.
Most pets eat only certain kinds of foods. A very few pets (boars and bears) will eat food from every major food category. With the introduction of 'raw fish' and 'raw meat', no pet eats every kind of food. See the hunter pets page for a table with a detailed listing of what pet eats what food.
To feed a pet a food item:
Left-click on the Feed Pet icon (in your spellbook or if you put it in your action bar), then left-click on a food item.
Drag a food item from your backpack and drop it onto either your pet's portrait or the pet itself in the game world. Besides being very intuitive, this has the advantage of avoiding the use of a Feed Pet icon button/keybinding/macro.
Do not simply left-click the food you wish to feed your pet. This will either do nothing, or if the food is edible, you will begin eating it instead.
Food for pets can be purchased from vendors. Vendors typically only sell one type of food, and finding the specific vendor(s) your pet needs can be problematic for pets with restrictive diets.
If your pet eats fish, cooked fish is amazingly cheap at fishing-related vendors.
Pets which eat meat will eat the meat which is not food to you (no health restored tooltip), uncooked (for example, Mystery Meat).
You can take up Fishing and choose a pet that will eat fish. This will save money as compared to buying food, but fishing can take a lot of time, so there's a trade off.
Feed Pet used to function with the happiness and loyalty mechanics. The pet's happiness and loyalty would increase their ability to fight which impacted how rapidly the hunter gained experience levels. Pets got a bonus to their base abilities when happy and a negative to their base abilities when unhappy. If either of those two pet stats dipped below certain default percentages, the pet would become unruly and would eventually abandon the hunter. Thus, it was vital to the use of a pet to have the ability to feed the pet.
The ability used to be gained by completing the hunter training quests, now removed.
Patch 4.1.0 (26-Apr-2011): Changed from Feed your pet the selected item. Feeding your pet increases happiness. Using food close to the pet's level will have a better result.