The major averages rallied to finish the week higher and post their best monthly gains since 2020.