Initially the weights are randomly initialized. During training the model processes data, computes an error (loss function), and an optimization algorithm (e.g. Adam) updates the weights along the gradient to minimize the loss. After training the weights are frozen and used during inference; they can also be further fine-tuned or compressed (quantization, pruning).
A model’s knowledge and capabilities must be stored in a form that enables inference, further training and distribution — this is the role of the weights.