The meaning means if the outcome of a situation or undertaking is a happy one, that makes up for any earlier unpleasantness or difficulty.
John Heywood, who wrote plays for the royal court from the early 1530s onwards, some sixty years before Shakespeare made his way in the Elizabethan theatre. Heywood also wrote a book of proverbs, including the now well-known sayings ‘out of sight, out of mind’, ‘two heads are better than one’, and ‘all’s well that ends well’.