The simple answer to that question is when management thinks the stock will increase in value. There are other alleged reasons, of course, such as to offset shares (dilution) issued via stock option exercises or for acquisitions or to just decrease the total number of shares outstanding. Remaining shares will have a larger claim on future cash flow with fewer shares outstanding.
Of course, management has not always been on the right-side of the trade. Stock prices do decrease. Management, with the benefit of hindsight, should have kept the cash in the coffers, re-invested in the business, or distributed to shareholders in the form of dividends. At any given moment, these strategies may be better than buying back shares – even if the stock price increases.