Hi there,
I suggest you take a look at the course content again since the underlying concepts are explained really well. And they are important to understand since several future concepts will build upon these basics.
A cost function provides a metric you can use to improve your optimization. When fitting a model you actually want to minimise the costs, (the model error). See also:
Gradient descent is a powerful method for above mentioned optimization when fitting your model. It is really well explained by Andrew Ng in this video:
Also this thread might be interesting for you:
Best
Christian