Why do all the planets and stars in our solar system have such similar average densities?

I don't think there is any fundamental reason beyond the physics of their formation and the physics that supports them.

Giant planets are formed mainly from hydrogen and helium. They are not formed of heavier elements because there aren't enough of them in a protoplanetary disc. Small planets are made of heavier, less volatile elements that condensed nearer to their stars. They are too hot and their gravity too small to retain hydrogen and helium.

Given that, we might expect the planets to follow two different mass-radius trajectories. The smaller planets basically have the density of what they are made of, with only a small amount of compression that depends on their mass. The gas giants are governed by an electron-degenerate equation of state that approximates to a $n=1.5$ polytrope. In the relatively low density regime (compared with a white dwarf) this results in an almost mass-independent radius and therefore a density that grows linearly with mass.

The two regimes are seen clearly in a mass-dependence plot for Exoplanets. Exoplanets mass-density

As you can see, some Exoplanets are much denser and the overall spread in density is more than two orders of magnitude. Thus the reason the planets in the solar system have similar densities is that (a) rocks do have similar densities and rocky planets are generally not massive enough to compress them much further and (b) the gas giants in the solar system are not very massive and the low-mass end of gas giants that can form have an average density similar to rocks.

There is further discussion that could be had about why (a) and (b) are true, which I may add to.


Given this table, the most signifcant factors affecting the density of elemental matter (matter organised into the elements) seems to be

1) whether the matter is gaseous or not, which seems to be a factor of around 50.

2) the average number of nucleons in a molecule of the gas (factor about 100) or non-gas (factor approx 50)

It is therefore suggested that the formation of the solar system and planets occured with either a relatively random distribution of these factors; and even some effects by which they act in a compensatory manner (such as described below).

In terms of composition the most abundant elements in the solar system are hydrogen and helium, covering 98% by mass. Due to the formation of elements in the early universe the most abundant elements all reside in the atomic mass range 1 to 28 (and the even numbered ones are more abundant) (as shown here). So density variation above 30 due to composition would be remarkably inconsitent with our theory of the formation of the universe.

Lighter elements need a stronger gravitational field to be retained, so it is only larger planets that can retain them. [For example, that's one reason we need to be careful about helium usage on earth, since it is inert it is not retained in compounds, and if released travels to the top of the atmosphere where it is blown away by the solar wind.]

Consequently it is smaller planets that tend to retain mainly heavier elements/molecules and are denser. Venus being an interesting because it is a small inner planet, but does have an atmosphere. Its remaining atmosphere is mainly $C0_2$, which has a high molecular atomic mass of 44, and is very around 100 times more massive and dense than the earth's.

As well as large planets in larger solar orbits retaining more lighter elements they have interiors that are under immense pressure; high enough to liquify some light gases. For example Jupiter is theorised to have a layer of liquid metallic hydrogen within it, one that extends out to perhaps 78% of the radius. Liquid hydrogen is nearly 800 times denser than the gas, (70.85 g/L, compared to 0.08988 g/L). By comparison the heaviest element, iron, has a density around 7800g/L (normally quoted as $7.8g/cm^3$), so this moves consideration moves us in the right direction but other effects are probably also involved. Note: Iron is also the most abundant element on earth, making up about 38% of its mass.

Summary

One effect is that tht if the body is massive enough to retain molecules of the light elements, then it is likely very large as light elements are the most abundant. Consequently it is likely that interior pressure will liquify a significant portion of these molecules, bringing the overall density back closer to that of smaller "rocky" planets that only retained solids (and maybe very heavy moleculed gases).