There are two answers to this question, depending on the definition of “the universe.”
One answer is that the universe is infinite, that it consists of simply everything: all matter, dust, planets, stars, energy, time, and space. Nothing could lie beyond the boundaries of “everything”; so, by simple semantics, we know the universe is infinite.
But what if one means the observable universe, all the matter and energy in it that you might detect if you were close enough to it? How do we know how widely those “contents” are spread? That has a more interesting answer.
The observable universe is about 40 billion light years across. One light year is the distance light travels in a year, or about 6 trillion miles; so the total distance is about 240 billion trillion miles.
We know the size of the observable universe because we know when it began. Cosmologists have learned from watching the galaxies that the universe is expanding; the stars, planets, and galaxies are all moving away from one another.
The current explanation is that the universe began with a tremendous explosion, the “Big Bang” theory, and that we on Earth are riding on a piece of the scattering debris.
By computing the speeds of the galaxies’ motions and determining the point from which they all seem to be scattering, cosmologists have figured out how long ago the explosion must have taken place for the scattering to have progressed this far. The Big Bang took place about 20 billion years ago.
Light travels faster than anything in the universe, so the remotest cosmic debris must be the pulses of light energy that shot away from the Big Bang in every direction 20 billion years ago.
By now that light has traveled 20 billion light years; light pulses traveling in opposite directions from the “Bang” are now twice as far from each other as from the point of the explosion, or 40 billion light years from each other, and that is the diameter of the observable universe.