If it's just temporarily out of stock and will become available in due course, just put up a big notice on the page to say "temporarily out of stock" and let people know if they can pre-order or what the timescales might be for getting more in stock.
If the product has been discontinued and there's an exact like-for-like replacement, you might do best to just redirect the old page to the new one.
If the product has been discontinued and there's a choice of possible replacements, say that – and give links to the new products.
If the product has been discontinued and there's no suitable alternative, then just put a "Sorry" notice on the page and direct people to your catalogue to look for other things.
Obviously when you have a product that has stopped and is not coming back, you need to remove it from your catalogue and get rid of all links to that page from elsewhere on your site!