If the universe continues expanding forever, how long until it’s too diffused to allow star formation? How many generations of stars will that mean the universe has? How will the elemental make-up of these late stars be different and what effects will that have?

I can’t give you an exact timescale but it’s possibly trillions of years in the future. That would happen when Dark Energy is as strong as gravity. We don’t know the dynamic of dark energy well enough to pinpoint with precision. As long as gravity is strong enough to keep a galaxy from falling apart, star-formation can (theoretically) continue.

A star is formed when a gas cloud undergoes the Jeans Instability. That occurs when when the internal gas pressure is not strong enough to prevent the gravitational collapse of the cloud. The instability is related to the density and the temperature of the gas cloud. Star-formation only happens if the gas is cool enough.

Young galaxies are gas rich and the gas cloud tends to be cool enough to have intense episodes of star-formation. During the star’s life-cycle part (or in some case most) of the gas is expelled in the interstellar medium (ISM) and the cycle begins anew. Big stars have short lives and they end in spectacular supernovae. Supernovae replenish the ISM but they can also create a feedback mechanism heating up the gas and substantially quenching the star-formation. Feedback happens also when a galaxy has an active supermassive black hole at its centre. Galaxy wide winds stops new stars from being born by dispersing and heating up the gas. Older galaxies are usually only passively evolving and have a fraction of the star formation of galaxies like ours, which produces about 1 star that has a similar mass to that of the Sun per year.

All things considered the universe has probably another few generation of stars in it. It had three generation of stars called Population I, II, and III in reverse chronological order. Population III are the original stars and were made almost exclusively of Hydrogen. Population II stars are very old stars and they are made of Hydrogen and Helium. The youngest stars are Population I stars. They have more metals (meaning anything that’s not hydrogen or helium *ducks to avoid chemistry book being thrown at me*) which make them redder and colder. The metals also influence their sizes making them smaller, which means eventually supernovae will become extinct and stars will just age passively and not die spectacularly.

So, just before the Dark Energy takes over, our galaxy would look like a dark red elliptical galaxy, like the artistically reddened one in this picture: