Wal-Mart will buy Jet.com for about $3 billion, the largest-ever deal for an e-commerce startup, as it races to build a web business to compete with Amazon.