The scientific name for the twinkling of stars is stellar scintillation (or astronomical scintillation). Stars twinkle when we see them from the Earth’s surface because we are viewing them through thick layers of turbulent (moving) air in the Earth’s atmosphere.
Stars (except for the Sun) appear as tiny dots in the sky; as their light travels through the many layers of the Earth’s atmosphere, the light of the star is bent (refracted) many times and in random directions (light is bent when it hits a change in density - like a pocket of cold air or hot air). This random refraction results in the star winking out (it looks as though the star moves a bit, and our eye interprets this as twinkling).
Stars closer to the horizon appear to twinkle more than stars that are overhead - this is because the light of stars near the horizon has to travel through more air than the light of stars overhead and so is subject to more refraction. Also, planets do not usually twinkle, because they are so close to us; they appear big enough that the twinkling is not noticeable (except when the air is extremely turbulent).
Stars would not appear to twinkle if we viewed them from outer space (or from a planet/moon that didn’t have an atmosphere).