PCs are a high-volume, low-margin business compared to enterprise IT infrastructure and services. Low margins are because PCs are getting commoditized i.e., PCs from most companies tend to have similar features (build, OS in the form of Windows) and hence customers start choosing PCs based on price. Only Apple has high margins as they have an integrated hardware & software ecosystem and also a better product, which allows them to charge more.
IBM decided to sell off their PC division as they shifted their focus to higher-margin IT infrastructure (servers, storage and other building blocks of cloud computing) and services (usually packaged along with the hardware). Also IBM has rapidly expanded their consulting business, which is another growth driver for IBM
HP tried imitating IBM, but HP's stock price took a beating. Consequently, Leo Apotheker , the then CEO was fired and HP decided to keep its PC division. IBM did the transition in a gradual planned manner, whereas HP tried to do it abruptly and hence it failed.