Well-posedness criteria in optimization with application to the calculus of variations