I am just learning about regularization and I haven't found a satisfying answer to this question. When I perform regularization, how come the **right** coefficients get decreased or turned into $0$?

I just learned that for *ridge regression* we add the term:

$$\lambda \sum\_{j=1}^p \beta\_j^2$$

to the cost function. $\lambda$ is the regularization parameter, $p$ is the number of predictors and $\beta\_j$ are the coefficients of the model's parameters.

I learned that in this type of regression the coefficients can be decreased, but they are never quite $0$.

In the case of *lasso regression* we add the term:

$$\lambda \sum\_{j=1}^p |\beta\_j|$$

to the cost function. Unlike ridge regression, here the coefficients can be turned to $0$.

So far so good. What really perplexes me, seems like magic, is how come the **right** coefficients are decreased/turned into $0$, and the **right** coefficients are increased/left out as they are? How come regression doesn't decrease a coefficient that would actually improve the model and that we would actually consider to be important? How come it doesn't make a mistake like that? I mean, we just added a term to the cost function. It seems too simple to work, to do the right thing. How does regularization know **which** coefficients to mess with and which coefficients not to? I don't see anything in the math of regression that tells this algorithm which coefficients to decrease. How does it know?