My understanding is that the stars are primarily made up of hydrogen and helium. Why do those elements end up in stars and other heavier elements make up the planets?
Most of the planetary mass in our Solar System is also in the form of hydrogen and helium - locked up in Jupiter. So I think your question is just confined to the inner, rocky planetary objects, Mercury, Venus, Earth and Mars, which are indeed very poor in hydrogen and helium compared with the Sun.
The gas from which the Solar System formed was 99% by mass hydrogen and helium. The heavier elements are trace contaminants. The measured elemental abundances of the Sun are pretty much representative of this initial composition (bar lithium).
The reasons that H/He are dominant are the limited window of time in which primordial nucleosynthesis can occur after the big bang; that elements heavier than helium are not produced (there are trace quantities of Li) by primordial nucleosynthesis, only in stars; and that most primordial gas has never been incorporated into stars. See Why didn't the Big Bang create heavy elements? and Why is hydrogen the most abundant element in the Universe? for details.
The sequence of events that leads to the inner planets being "rocky" is that they are formed from solids that were able to condense at the high temperatures close to the Sun. Most of these solids would have been things like metal oxides or even solid metal grains that did not incorporate either hydrogen and helium (with a few exceptions). The vast majority of hydrogen and helium would have remained in the gaseous phase. The solids then initially stick together by being... sticky! Subsequently, once the protoplanets get massive enough, gravity takes over and the bodies can begin to accumulate matter and become even bigger.
It is a relatively straightforward calculation to then show that objects the size of Earth and at the temperature of the Earth are not able to retain gaseous hydrogen and helium - the typical speeds of the atoms/molecules is big enough that the gas can escape from an Earth-like planet. Thus, unlike the gas giants which are both more massive and in a colder part of the Solar System, the inner planets end up with very little hydrogen and helium; and what there is exists bound up in molecules with heavier elements (e.g. water).
It is not that the heavier elements prefer the rocky planets instead of the star. It is H and He that escape easier from celestial bodies with lower gravitational potential, as is the case of that planets.
The Sun for example has only about $1$% of other elements besides H and He. But that small fraction is still 1000 times the mass of the all the inner planets together.
The universe is almost all hydrogen and helium (about 98%), so of course the stars are too. You can get smaller clumps of heavier elements because they can stick together and form solids, but, as The_Sympathizer said, there's no natural way to separate out enough oxygen or whatever to put together a star.
The fact that hydrogen can escape lower mass planets isn't really the reason, I would argue. That's just the reason that such planets don't have hydrogen atmospheres.
Finally, don't get the impression that other elements are somehow excluded from the stars. The sun is 0.014% iron, but that works out to 46.6 planet earths made entirely of iron.
Stars are not made of other elements simply because they are rare and not concentrated enough naturally to make them.
There is nothing impossible about, say, collapsing a cloud of carbon to make a star that burns carbon in its core. Problem is that free clouds of pure carbon with enough mass to do that just don't exist. Instead, we have free clouds of almost entirely hydrogen and some helium. Heavier elements are mixed with those things, and thus as a result, they don't get a chance to collapse into large stars without also collapsing in way more hydrogen and helium.
And with planets, once you get past a certain mass, it starts accruing more of the hydrogen/helium dominant components of the stellar birthing nebula or, more accurately, the disk of such material around the star, so it either becomes a gas giant, or else gets so big it becomes another star in its own right made with the usual composition, i.e. a binary or multi-star system with a small companion star next to, and orbiting, a large one.
Hence, if one found a star made of some such exotic composition, it might be a good indicator of aliens having done it artificially!
This is somewhat a separate line of consideration than the "planets' gravity keeps heavier elements" aspect the other answers provided, but note that stars are limited in what elements are provided in the primordial cloud that forms the star as well as the elements they produce via stellar nucleosynthesis.
So, to some degree, stars contain primarily hydrogen and helium because that is primarily what is found in regions of star formation. Then when enough hydrogen turns into helium (or carbon, oxygen & nitrogen), the star begins helium fusion which forms to make carbon and so on down the line. Eventually, for the heavier stars, we get to silicon fusing to create iron, which unfortunately coincides with the end of that star's life (cf. this Physics.SE Q&A). For the smaller stars, however, I believe a helium core is all you can expect and the star basically lives its very long life really only burning those two.
The end of the star's life is a fantastic explosion (supernova), which means that the material that was the star is now flying (at some fraction of the speed of light!) out into space and isn't (strictly) localized to the star. It is during the supernova process, we begin the fun neutron capture processes that give us the remaining elements we see in planets, comets, etc via either the rapid process or the slow process (see also supernova nucleosynthesis).
So it is a bit of a feed back loop at this point: the (predominantly) hydrogen cloud collapses into a star, the star produces heavier elements & explodes, the supernova produces some even heavier elements while also pushing the outer hydrogen layer back into the cloud to later create a new star.
It is an interplay of several factors:
- Big bang nucleosynthesis produced almost only hydrogen and helium (3/1).
- Under Gravitational contraction in the early universe clouds of these gases concentrated. What we define as a star is a collapse of a gas cloud under its own gravity that simultaneously is so much concentrated and hot that increases its internal temperature to a degree that fusion begins and outwards pressure equates gravity.
- Onion like structure of burning elements begins, rate is determined by the mass of the star. Burn stops at Fe, the heavier elements are constructed more rapidly when stars terminate their life abruptly. Their concentrations are pretty low though since every heavy element needs its predecessor in the periodic table, roughly speaking, in order to form. Hence can’t have heavy concentration of an element that is not abundantly produced
Long story short, that’s the reason for stars chemical composition.
Now for planets, can’t have rocky planets made of gases by definition. More seriously, light elements evade easier planets weak gravity. However planets made of gases exist. Most are failed stars, their gravity was not enough to start nuclear fusion and they end up like the gas giants in our solar system.