The simplest way is to get a piece of the planet and look at it, but in reality this doesn't happen very often. We sent astronauts to the moon and they picked up rocks and brought them back, so we have pieces of the moon to study. We also have a few pieces of mars and the asteroid belt that have fallen to the earth as meteorites. But what about the rest of the universe?
Well, we can send spacecraft to the planets. Man-made robots have landed on the moon, venus and mars and studied their surface. Spacecraft have also orbited or passed by all of the planets, and there is a spacecraft on its way out to study Trans-Neptunian Objects. We have also dropped atmospheric probes into planets like Jupiter that are made of gas. Spacecraft that land on the planets or enter their atmospheres can use scientific instruments to find out what the planet is made of.
We can also observe objects from afar and learn a lot about them by just studying the light that they give off, or that they reflect.
In almost every case, whether it is an instrument actually on the planet, or a telescope looking up from the earth, scientists use some variation of an instrument called a spectrometer. Spectrometers take a signal from whatever they are looking at (whether it is a rock, or a cloud or a whole planet or a star or a galaxy or a nebula, etc.) and spread the signal out into its components. Most spectrometers work with light and are a lot like extremely good prisms; they take the light coming from some object and separate it out into its colors. This is useful because it turns out that every element on the periodic table only gives off light of a few certain colors. So if we spread out the light coming from some object and see only certain colors, then we can match thoses colors to the elements that produce them. It's as if everything in the universe has a hidden fingerprint that we just need to learn how to read.
Some spectrometers work on things other than light. For example, a mass spectrometer takes a mixture of chemicals and separates them according to their weight. Other spectrometers measure invisible forms lof light like infrared or x-rays. The idea is always the same, though.