Astronomers know how many stars are in the universe because they are pretty certain they can tell how big it is.
They know how big it is because they think they know how old it is, although there is some disagreement on that point.
And estimates of the universe’s age in turn depend on estimates of the speeds and distances of the farthest stars we are able to detect. This question’s answer is a good example of the interconnectedness of astronomical knowledge.
The reasoning goes something like this. The farthest galaxies whose energy reaches our telescopes on Earth are flying away from us at nearly the speed of light. This is shown when their light is broken down in a spectrograph, an instrument that measures the wavelengths of the light reaching it: there is an extreme “red shift” in the spectral lines made by the various elements making up the stars.
Astronomers have concluded that this means the universe is expanding, blowing up before our eyes. The amount of the red shift from the far galaxies tells us both how fast they are receding from us and how far away they are.
If we know how far from one another the pieces of the universe are, where they are heading, and how fast they are moving, we can apply the laws of motion and determine when they must have exploded to be flying away at their present speeds. The commonly accepted estimate for the Beginning, the Big Bang that created everything, is between 10 and 20 billion years ago.
How wide is a universe that blew apart, say, 20 billion years ago? Nothing we know of travels faster than light, so the light energy from the Big Bang itself must be scattered the farthest of anything in the universe. Since light travels at 186,000 miles per second, or one light year in a year (6 trillion miles), the light generated at the moment of Creation must have progressed 20 billion light years in all directions since then. So the matter and energy in the universe must have a radius of at most 20 billion light years, or a diameter of 40 billion light years.
By judging the vast distances of space, astronomers have found that the galaxies they are able to detect are on the average 100,000 (105, or 10 x 10 x 10 x 10 x 10) light years in diameter, and that the distance between stars is about 5 light years. So the number of stars that can fit in a galaxy of an average spiral shape is about 100 billion (1011).
The galaxies are about 2 million light years apart in our corner of the universe. It seems reasonable to assume that the universe is approximately spherical, if it was formed by an explosion, and that its debris is distributed with approximate uniformity.
Thus, in a spherical universe with a maximum diameter of 40 billion light years, there is room for about 10 billion (1010) galaxies. So in 10^10 galaxies, each containing about 10^11 stars, there are at most 10^21 stars; or about a thousand billion billion stars in the universe.