Teachers unions in recent years have flexed their muscles to secure contracts with better pay. But now many of the same districts, including Boston, Newton, and Marblehead, face cuts and layoffs.