A major reason for one firm to buy the stock of another firm is as a means to move towards merging with or acquiring that company.
It is not unusual for two firms to merge or for one to acquire the other. Firm A, for example, might believe that it can make more money if it acquires Firm B. It might believe that it can acquire Firm B, fire many of its workers (by getting rid of people who are duplicated in Firm A's work force), and still produce as much revenue as A and B together used to produce.
As a step towards acquiring Firm B, Firm A would typically try to buy up as many of Firm B's shares as it could.