The goal of mathematical optimization is to find the maximum and minimum in the whole function domain. If the function is continuous in the closed interval, there are maxima and minima in the whole region through the extreme value theorem. In addition, the maximum (or minimum) on the whole domain must be a local maximum (or minimum) within the domain, or it must be located on the boundary of the domain.
Therefore, the way to find the maximum (or minimum) in the whole domain is to look at all the local maximum (or minimum) inside, and also look at the maximum (or minimum) of points on the boundary, and take the maximum or minimum.