Why is the number of cells virtually a constant?
You have to understand this is a very, very, very, very broad question. Decades of research in multiple disciplines allow us to answer this question in a million directions. However, I would start by looking up information about the cell cycle and its checkpoints. Here of course I would point you - for starters - to this page:
Khan Academy's page on cell cycle checkpoints
For further reading, you can consult the college-level chapter on Cell communication and cell cycle regulation. Here the most relevant section would be Regulation of cell cycle, it's a great introductory read, well researched and well presented didactically.
A short tl;dr
In short, cells have checkpoints at different stages of their life cycle - both before and after they divide. You can imagine that, for instance, a nutrient starved cell incapable of copying its DNA prior to division, will arrest its progress through the cell cycle at the checkpoint before S (DNA synthesis) phase. And under the right conditions, it will resume its progress and get ready for division. This example is only one of many checkpoints that exist, and varying types of cells may have varying checkpoint requirements.