Light
Each atom absorbs specific wavelengths of light dependent on the element. So if we look at Jupiter’s spectrum, we will see dark lines where Jupiter’s atoms absorb light. By analyzing what wavelengths are these lines we can tell which atoms Jupiter is made of, or at least which atoms can be seen near its surface.
We can also deduce the mass of celestial objects from their gravity interactions. Knowing the mass and size of planets allows to calculate their average density, which helps to deduce what they are made of.
Also we have already landed spacecraft on at least some of the planets and drilled some holes into them.
Scientists analyze the light reflected from the planet and compare that result to that of pure, known gases on Earth. When the analyses match perfectly, they know the gas on the other planet matches the one they tested on earth.
One thing to add here: our knowledge of physics and chemistry, while not perfect, is actually really good. We have a pretty good idea of what most matter is, how it’s formed, how it interacts with other matter. So for an object like Jupiter, if we have a viable model of its composition that’s consistent with all our observations (the composition of light, gravitational effects, etc.), it’s usually a pretty good/useful model—it’s a lot more than just guesswork. Positing new kinds of unseen matter (e.g., a novel gas) generally creates more questions than it answers.
Dark matter and dark energy on the other hand…