The loss function is how the model knows it is wrong.
Without it there is no direction to improve in.
It takes a prediction and the truth and returns one number.
Bigger means worse, and zero means perfect.
Training is simply the search for the smallest number it can reach.
Which loss you choose changes what the model cares about.
Squared error cares enormously about large mistakes.
Absolute error treats a miss of ten as ten times a miss of one.
So squared error chases outliers while absolute error mostly ignores them.
For classification the usual choice is cross-entropy.
It rewards being confident and right, and punishes being confident and wrong.
Being unsure and wrong is punished far more gently.
The loss is what you optimise, but not always what you actually care about.
You might optimise cross-entropy and then be judged on F1.
When the two disagree, the model follows the loss and not your intentions.