I am making a tomato sauce for pasta but I am not sure whether to put the garlic and onions into the sauce raw or to sauté them first?
What would be the difference in the taste, does one have more depth?
Sauteing garlic: garlic needs very little sauteing, far less than onion
So you get more depth of flavor, better flavor and texture sauteing them.
You can find out.
Split your sauce into 2 batches. Add onions and garlic raw to one and sautéed to another. Some people like the sharp strong taste of those things raw. In the US that is not common. Sauteeing will mellow the taste. When I make sauce I sauté it because I hope someone else will eat it besides me.
If you are not sure or you are American consider 1/3 sauce with the raw and 2/3 with the sautéed.
You can also use ‘layering’, particularly with the garlic. Garlic fried in oil at the beginning of the process contributes rich, mellow, savory flavors, but a little finely minced or shaved garlic added right near the end adds a sharper, fresher, more ‘forward’ garlic flavor. I guess you could do the same with onion, though I wouldn't add it as close to the end as I do with garlic.
Many of the flavors in garlic are oil-soluble, not water soluble. In fact, you can just crush garlic cloves, saute in oil, and then remove the garlic completely. The oil will taste strongly of garlic, as will the final dish. Sauteing your garlic will help these flavors be more apparent in the tomato sauce (the flavors will also be slightly changed, as other answers describe).
Onion: Sauteing the onions lightly for a few minutes, till translucent, will mostly eliminate the very harsh onion taste that raw would contribute.
One popular (I hesitate to say "best", but certainly my favorite) way to make a tomato sauce is to lightly saute the onion in a small amount of olive oil in your sauce pot for about 3 minutes, then add finely chopped garlic for about 30-60 seconds (before it burns). Now is a good time to add any dry spices you want, to toast them and make them more effective, then add the tomato to the same pot. That way the oil (which is now carrying significant amounts of flavor) is incorporated into the final product.
In European and Mediterranean cooking traditions, it's unusual to find raw onions added into a sauce which is cooked or served warm. As GdD alluded to, onions (and other aliums) have harsh, not-entirely-pleasant flavors and odors which become very apparent when they're warmed, and which are removed by sauteing or other dry cooking. (Think of the odor of a cut onion that's been left out for a while.) There are sauces which use raw onions -- pico de gallo and chimichurri come to mind -- but these are not cooked, served cold, and meant to be sharp and aggressively fresh-tasting. When in doubt, saute your onions at least until translucent. The same is true of garlic, though in my opinion it's less crucial to saute garlic than onions if you're going to be cooking the sauce.
Follow the recipe, and failing that, always sauté first. As noted by previous commenters, sautéing accomplishes a number of things. Overall, softening is not that important, they will soften while simmering in the tomatoes. But the release of sugars is. Also the browning and flavor that comes with that (though some recipes call for sautéing only until translucent). There are a number of chemical reactions that occur when sautéing that don't occur, or occur much more slowly when simmering in liquid. If you put raw onion into a tomato sauce, you are introducing a lot of liquid that would have evaporated while sautéing. And its harder to reduce a tomato sauce, because for a good sauce you simmer very gentle. Thus, sautéing is also a much faster way to reduce the liquidity of the final sauce.
Finally, garlic is a special case. I don't know the chemistry, but experience has shown me that garlic oil outside of the garlic does not break down and mellow as well as it does inside the garlic. Sautéing helps mellow that bite, and as a previous commenter said, it takes mere minutes. If you pureed that same amount of garlic into the sauce, then simmered, you would never get rid of that bite. (I suspect the issue is temperature, you will never get a sauce above 100C/212F without a pressure cooking, you can sauté much hotter) Some recipes want that bite, which is why I say follow the recipe.
Great question! The previous answers are great but have left out an important detail for those new to the process: be careful with the pan temperature! Especially with garlic, you want only moderate heat. If you're following @nexus_2006's excellent advice of adding dry spices to the oil and garlic (if only salt and fresh ground black pepper), you still only want moderate heat. This goes double for most savory spices, like ground sage, chili powders, nutmeg, etc.
Onions can go high or moderate heat, but the results will be different. If you like your onions a bit blackened, go with the higher heat. Slightly blackened onions can be great with meats, e.g calves liver, lamb kidneys or steak tips, but is not recommended with a tomato sauce.
If you're making the tomato sauce from scratch (i.e. fresh, whole plum tomatoes, not from a can), be sure to give the tomatoes time to cook down before adding any fresh garlic. Usually, about 1/2 hour before it's done I add some fresh garlic and other vegetables that shouldn't be over cooked (anything green, red bell peppers, etc.).
Get the temperature right and you'll present either option in its best light.