This lecture covers the concept of using a least squares procedure to fit a function, incluing the idea of minimizing the sum of the squared error, and finding the coefficient of determination (i.e. R-squared).
The video below covers this material as well as examples in Excel and Python.