Amazon.com Inc. said on Friday it would buy Whole Foods Market Inc. for $13.7 billion, including debt, instantly transforming the online giant into a major player in the bricks-and-mortar retail sector it has spent years upending.

The acquisition, Amazon’s largest by far, gives it a network of more than 460 stores that could serve as beachheads for in-store pickup and its distribution network. It would make Amazon an overnight heavyweight in the all-important grocery business, a major spending segment in which it has struggled...