Companies have the obligation to shareholders to make the best use of all of their assets. If the opportunity arises to invest in the stock of another company, it may be for several reasons.
A company wishing to acquire shares of another company's stock may do so with its own stock or with available cash on hand. Debt financing is arranged in many cases, but any of these methods is usually employed when the acquiring company wishes to enhance its own corporate performance and growth.
When a company becomes so large that it's cash flow can't be reinvested into its own growth, its pace of earnings growth will slow. Cash levels will accumulate. This cash can either be paid out in the form of dividends to shareholders or used to buy shares in smaller, high-growth companies.
Companies may also merge when it becomes clear that the two acting as one can share economies of scale, and--again--add shareholder value by increasing earnings-growth potential.
A company can use available cash or credit to purchase shares of another company, but only up to a limit. Once this limit is reached, it must state how much of the acquired company it owns and whether it plans to buy the remaining shares. This is known as a tender offer.
If the buying company states intentions to buy the target company, the target company may agree, recognizing the benefit to both parties. If the target company finds this is not it its best interests, it may take a number of measures to avert the purchase of its shares. But the acquiring company is attempting the takeover in order to increase its long-term shareholder value.