A loss function measures the model's prediction error by comparing its output to the actual target values. During training, the model aims to minimize this error, adjusting its parameters to improve accuracy.