In test matches, the bowling teams can ask for a new ball after the old one has been used for 80 overs. Usually a team that does so will then use fast bowlers in the hope of taking wickets.
In one-day games, changing to a new ball is on a fixed timetable. For example, in a one-day international with 50 overs per side, a new ball is used at the start of each innings and after 25 overs of each innings. The players don’t have any choice about the new ball. Some first class matches such as the English county championship have one new ball at the start of each innings, which can mean much more deterioration of the ball in a long innings.
In a test match, the bowling side has some control over the new ball. After 80 overs with the same ball, they can take a new ball whenever they like, though they don’t have to do so.
The main factor on when to take the new ball is the different types of bowling. A new ball will be harder and undamaged. This means it can bounce harder and more predictably. This makes it particularly suitable for fast bowling.
The old ball will have uneven damage, making its bounce less predictable. This means a team which wants to concentrate on spin bowling may not take the new ball straight away.
A team that is using seam/swing bowling may have been building up a shine on the ball by rubbing it on their trousers. This creates a disparity between the two sides of the ball, which can help with swinging the ball through the air. If this is the case and the tactic is working well, the team may not want to take the new ball immediately and lose this advantage.
The context of the game is also important. In the first innings of a test match, the 80th over will usually come near the end of the day (scheduled for 90 overs.) This means the batters may be tired and the light may be fading. This can make fast bowling with a new ball an attractive option, so teams very often take the bill right away.
If the opponents are down to their tail enders (the last few batters in the order), the bowling team may take or not take the new ball depending on whether they think the remaining batters are more vulnerable to fast bowling or spin.
If a ball is lost or damaged, regardless of the game type, it will be replaced. Ideally it will be replaced with a ball that has been used for a similar amount of time as the lost and damaged one. How easy this is to do will vary depending on the level of cricket being played. At the top professional level, the umpires will have a variety of balls used for different amounts of time or with differing levels of wear and tear. This can vary however: a ball used for 40 overs in a T-20 match may well have taken a lot more damage than one used for 40 overs at the end of a test match where the batting team were playing defensively.