Northeast Health System, which operates hospitals in Beverly, Gloucester, and Lynn, has agreed to affiliate with Lahey Clinic of Burlington, a teaching hospital affiliated with Tufts Medical School, in the latest hospital merger in Massachusetts. The deal, which will create a new umbrella company called Lahey Health System, could affect other potential consolidation moves. Lahey had begun preliminary merger talks with Boston's Beth Israel Deaconess Medical Center, a Harvard-affiliated teaching hospital that had also submitted a bid for Northeast. Lahey and Northeast are both nonprofit organizations. Two for-profit hospital chains, Steward Health Care System and Vanguard Health Systems, had also initially submitted bids for Northeast, though Boston-based Steward ultimately dropped out of the competition.