Are all stars inside galaxies? No. There are some stars that are not part of a galaxy. Stars can form outside a galaxy, or they can be kicked out of a galaxy by something. They are called intergalactic stars. Stars that form outside of a galaxy can also be attracted into a galaxy.
Stars are formed out of clouds of gas and cosmic dust that are attracted by gravity. The particles start to clump together, and those clumps attract more clumps, and so on until something the size of a star forms. When it is big enough and has enough eternal pressure, nuclear fusion can begin and, voila, we have a star. Most stars are in galaxies because they form out of the same gas cloud. Most of space is filled with very small amounts of particles. In some places, these particles gather into gas clouds and start to form stars. The whole gas cloud doesn’t form one star, but provides material for billions of stars. The process is ongoing because stars become supernova and explode, creating more gas and dust to make more stars. Then, a black hole forms and the center of the galaxy, and all of the stars orbit around it.
There is no limit to how many stars make up a galaxy. The Milky Way contains between 100 and 400 million stars. A galaxy called UCG 1885, which is 232 light years away from us, probably has over a trillion stars. On the other hand, the Sculptor Dwarf Galaxy, which orbits the Milky Way, only has about a billion stars. The smaller galaxies are probably absorbed by the larger ones over time.
Most stars form in galaxies because that is where most of the gas and dust has accumulated, but that is not the only case. Some stars can form outside or a galaxy, although it is probably fairly rare. Stars need gas and dust to form, but they need that gas and dust to be cool enough to clump together. Sometimes gas and dust is ejected from a galaxy. This can be thrown out by the supermassive black hole at the center of the galaxy or it can be blown off by winds from nearby galaxies. This provides material, but it is far too hot for a star to form. It needs to be allowed to cool down, but it disperses before it can.
There is a type of galaxy called a jellyfish galaxy. This type of galaxy forms when one galaxy travels at very high speed through a cluster of other galaxies. It is travelling so fast when it hits the other galaxies that the pressure causes a wind to blow through the moving galaxy. This is called ram pressure and it creates a very long trail of gas and dust from the galaxy. This tail is cool enough for stars to form and they do, occasionally. Once they have formed, they have no galaxy to orbit. A star that has no galaxy is called an intergalactic star.
Stars forming outside of galaxies can happen, but it is not a common cause of intergalactic stars. The most common cause is because a star has been pulled or pushed out of the galaxy it was born in. All of the stars in a galaxy orbit around the supermassive blackhole at the center of the galaxy, but the galaxies all orbit around each other and the points between massive clusters of galaxies. Because they are moving, the occasionally collide. Small galaxies sometimes get consumed by larger ones, but if the galaxy is large and fast enough, it can tear through another galaxy. When this happens, stars can be ejected out into interstellar space, becoming intergalactic stars. They float through space until they die a natural star death, or until they are absorbed by another galaxy. Considering how far apart galaxies are, it is extremely unlikely that an intergalactic star would live long enough to reach another galaxy.
An intergalactic star would be on its own, but, if it had a planetary system it would probably take them with it intact. And for anyone living on those planets, being part of a galaxy or being an intergalactic planet would make no difference. We are so far from the other stars in the Milky Way that more distance wouldn’t make any difference. We may get the chance to find out in about 4.5 billion years because the Andromeda Galaxy is on a collision course with the Milky Way Galaxy. It will take about 5 billion years after that, but the two galaxies will end up merged. Our sun and our planet won’t be here by then, but some stars will definitely be kicked out into space. And this is what I learned today.
Image By ESA/Hubble, CC BY 4.0, https://commons.wikimedia.org/w/index.php?curid=76167204
Sources
https://astronomy.stackexchange.com/questions/29397/could-stars-form-outside-of-galaxies
https://starchild.gsfc.nasa.gov/docs/StarChild/questions/question29.html
https://en.wikipedia.org/wiki/Intergalactic_star
https://www.skyatnightmagazine.com/space-science/stars-in-milky-way
https://esahubble.org/news/heic1719/
https://www.sciencenordic.com/astronomy-denmark-galaxies/how-are-galaxies-formed/1774357
https://astronomy.stackexchange.com/questions/17817/which-came-first-galaxies-stars-planets
https://en.wikipedia.org/wiki/Galaxy_formation_and_evolution
https://astronomynow.com/2020/01/06/godzilla-galaxy-one-of-largest-observed-hosts-a-trillion-suns/
https://www.skyatnightmagazine.com/space-science/andromeda-milky-way-galaxy-collision