Early in our cooking career and for a long time after, we assumed that you needed to cook garlic as long as its cousin, the onion. Our results were spotty: sometimes it was ok, sometimes we picked burned bits out of our dinner. It wasn't until we got serious about cooking that we learned another way...
Whether they're being used in a quick stir fry or as the base for a soup or sauce, both onions and garlic need to be cooked at least little to get rid of their raw bite. However, while onions benefit from a longer cooking time, garlic will quickly burn and become acrid if cooked the same way.
Garlic usually does best if it's cooked quickly and over medium heat. About thirty seconds will do the trick. This is just enough time to cook off the rawness, allow the flavor to mellow into the dish, and let the aroma to hit its peak. You'll know it's done when you can smell the garlic and your mouth starts watering!
To account for these different cooking times, start the onions first, cook all the other main ingredients, and then add the garlic last. We like to clear a little spot (or "clean slate") in the middle of the pan to let the garlic cook by itself for a few seconds before stirring it into the rest of the dish.
It seems counter-intuitive that a short cooking time would be enough to infuse the entire dish with garlic flavor, but it makes more sense if you think about garlic as another seasoning like ginger or thyme instead of one of the main ingredients.
If you've been cooking your onions and garlic together, try this technique next time and see if you notice a difference!
Related: Recipe: Roasted Garlic