International Business Machines Corp. agreed to buy software-and-services company Red Hat Inc. for about $33 billion in its biggest acquisition ever, a deal aimed at helping IBM Chief Executive Ginni Rometty boost a cloud-computing business central to her efforts to revive the tech giant.

IBM rivals Amazon.com Inc. and Microsoft Corp. have jumped ahead of it in recent years in the business of providing computing power and software for rent. But Ms. Rometty said in an interview that the market is moving into a second chapter...