Compass Inc. agreed to buy Anywhere Real Estate Inc. in a $1.5 billion stock deal that would cement Compass’s status as the largest residential brokerage in the US.