Galaxies are effectively their own ecosystems, made up of four major components: gas, stars, a supermassive black hole, and dark matter (which makes up 85% of the mass of the galaxy, and is most likely some sort of particle we haven’t yet discovered which has mass).
Each galaxy has a supermassive black hole at the center, likely created from density fluctuations at the beginning of the universe which then grew over time. One of the greatest mysteries of galaxy formation is why we see a black hole mass-galaxy mass relation; for some reason we see that the black holes in galactic centers are about 1/1000 the mass of the galaxy. This is observed over all galaxy types (spiral/disk, elliptical, and even less regularly-shaped galaxies), and more than 4 orders of magnitude in galaxy mass.
Because of this tight correlation, it appears that there is some form of self-regulating feedback dictating the amount of black hole growth. Black holes tend to grow from two primary mechanisms: hydrodynamical instabilities near the galactic center (primarily in disk galaxies), which drive gas inward, as well as galaxy mergers, which provide a new source of gas to fuel the galaxy black holes, which eventually merge.
These mechanisms create two types of black hole feedback. ‘Quasar-mode feedback’ is very intense consumption of gas by the supermassive black hole after two galaxies of similar size merge. This process shuts off star formation in the new galaxy as the gas is rapidly consumed (over several hundred million years). ‘Radio-mode feedback’ or ‘maintenance-mode’ feedback is fueled by instabilities, which create a slow-flowing stream of gas toward the galactic center. This process can last much longer, but also deprives the galaxy of gas for star formation in the long term.
Another mechanism by which gas is driven out of a galaxy is supernova feedback. Massive stars (initially greater than ~8 times the mass of our sun) explode at the end of their lifetimes, driving gas out of the galaxy. This feedback mechanism is important for galaxies which are vigorously forming stars, as the most massive stars in a galaxy die quickly (millions of years) in comparison to stars the mass of our sun or less (billions of years).
In a galaxy, infalling gas first ends up in the hot halo of gas surrounding the galaxy. As it cools, it either tends to flow onto the galaxy and trigger star formation, or flow onto the black hole. Feedback mechanisms eject hot gas back out into the halo, where it can eventually be recycled back into the galaxy.
In summary, galaxies are viewed as ecosystems, usually in simulations from the top down. On the top level, we have galaxy environments, which can dictate the fate of a galaxy, as galaxies tend to form in clusters (galaxy cluster/group size is a power law, with many galaxies forming in clusters of thousands of galaxies, and fewer forming in smaller groups; the universe looks like a dendritic fractal when we look at the density of material). They may interact with galaxies around them, often many times over the age of the universe, which shapes their ultimate gas supply, shape, and size. On the next level, we have the galaxy structure, dynamics, and gas flows, which dictate shocks and instabilities in the galaxies. These shocks and instabilities regulate star formation and supernovae in the galaxies; stars are generally viewed as the smallest component in the system (not counting gas flows, which vary widely in resolution depending on the simulation).