Why do onions and garlic burn so easily?
Because both contain a high proportion of sugars relative to water, and garlic in particular is small, thin and low in moisture — so it passes from cooked to burnt very quickly.
The chemistry. Onions are roughly 5–9% sugar by weight, mostly fructose, glucose and sucrose. Sugars caramelise at relatively modest temperatures, and once caramelisation begins it accelerates. Past a point, the sugars pyrolyse into bitter compounds, and that transition is not reversible.
Why garlic is worse than onion. Garlic is typically cut far smaller, so it has an enormous surface area relative to volume and very little internal moisture to buffer it. Chopped or crushed garlic in hot oil can go from fragrant to acrid in under a minute — and burnt garlic makes an entire dish bitter, in a way that cannot be cooked out or masked.
Crushing makes it worse still, since it releases more sugars and more surface area than slicing.
What to do about it:
Add garlic later than onion. This is the main answer, and the most common recipe error is adding both together. Onions take 8–10 minutes to soften; garlic needs 30 seconds to a minute. Cook the onion first, then add garlic at the end of that stage.
Add it to a wetter environment. Adding garlic after liquid or tomatoes have gone in removes the risk entirely, since the temperature is capped near boiling.
Lower the heat when garlic goes in.
Cut it larger where a recipe allows — sliced garlic is far more forgiving than minced.
Watch the colour. Pale gold is done. Any brown at the edges means stop.
If it burns, start again. Bitter compounds do not disappear, and attempting to rescue it wastes the rest of the ingredients.
Where the same problem appears: caramelising onions properly takes 30–45 minutes on low heat, and recipes claiming ten minutes are describing browning rather than caramelising.