How are stellar and galactic ages determined?
Stars
Indeed, the most readily apparent observables for stars are (1) their apparent luminosities, and (2) their spectra (or even just colors if you can only do photometry). The age has to be inferred, and this is where modelling comes into play.
The Vogt-Russell "theorem" is the assumption that the initial mass and chemical composition of a star uniquely determines its observables for all time. Stellar models are constructed for a variety of masses and compositions, and they are evolved according to the equations of stellar structure and evolution. A number of additional assumptions are usually imposed here, such as
- The stars are always very close to hydrostatic equilibrium (an excellent approximation in most cases);
- The stars are spherically symmetric, or at worst have small perturbations from spherical symmetry;
- The star is sitting alone in vacuum (though some models take a binary companion or accretion into account);
- Heat transport in convective regions is adequately captured by mixing length theory;
just to name a few. Despite all these assumptions, modeling is extremely complicated, as is attested to by the size and complexity of even 1D stellar evolution codes.1
Models can then tell us what a particular star will look like as a function of time, and we can invert this relation to infer age as a function of observables. Of course, having more observables is better, and several important pieces of information include
- Absolute luminosities, which are obtained in a number of ways, such as parallax for the closest objects;
- Masses, as can be inferred from e.g. binary star systems;
- Internal structure and vibrational modes, as can be inferred from changes in the surface (astroseismology).
Putting all this together, we can get a general sense for the ages of stars. However, there are many sources of uncertainty. For example, many parameters for these models are calibrated off the Sun, and there is no guarantee they are the same in all stars. Moreover, many stars sit around for billions of years with very little change in outward appearances, and so there is often a large range of ages that are consistent with the observables.
To get a sense of how the Sun is modeled to move through temperature-luminosity space (as plotted in an HR diagram), take a look at this video from UIUC. The purple line is the main sequence - the locus of points of varying mass corresponding to stars that are in their hydrogen core burning stages. Once this central fuel is exhausted, the Sun will proceed very rapidly through other phases, taking on temperature-luminosity values that cannot be expressed by stars of any mass or composition in the main sequence.
Globular clusters
You didn't specifically ask about globular clusters, but they are an important part of the story. Any one cluster will consist of hundreds of thousands of stars, and these are thought to have all formed at the same time and with the same composition. As a result, one can see a distribution of stars and their properties in a snapshot of a single age.
We can actually get the ages of clusters much better than individual stars, thanks to the aforementioned fact that stars "turn off" the main sequence at a particular age. This age is a monotonically decreasing function of mass, because more massive stars burn through their fuel much faster.2
This video shows the evolution of eight stars, from $1\ M_\odot$ to $8\ M_\odot$ in mass. An even more dramatic effect is shown in this video, which shows $10{,}000$ stars with some reasonably realistic distribution of initial masses.
For any given globular cluster, we can plot all its stars on an HR diagram and basically read off the age from the turn-off point. As it turns out, most of them are almost as old as the universe, forming less than a billion years after the Big Bang. In fact, this measurement caused cosmologists some concern in the middle of the twentieth century, when some models for the universe underestimated its age to the point where some clusters seemed older than the universe itself.
Galaxies
If you want to know the amount of time that has passed since a particular galaxy formed, you probably are not going to get a very precise answer. Just like globular clusters, galaxies are nearly as old as the universe. Unlike globular clusters, they may evolve in rather complicated ways via interactions with their environments. For example, we know there are streams of "pristine," primordial gas (i.e. gas that has not been enriched with heavy elements by stars) falling into galaxies. At the same time, material is being ejected due to everything from stellar winds to supernovae to jets from supermassive black holes in the galaxies' centers. Moreover, galaxies may indeed merge, so it becomes unclear what counts as the "birth" of a particular galaxy.
You could instead ask about the average ages of stars in a galaxy, or even in subsections of the galaxy. If there was a recent burst of star formation, there will be enough massive, hot, blue stars, making the overall light rather blue (as is often the case in spiral galaxies). If new stars haven't formed in a while, the massive ones will have died out, leaving just redder stars to dominate the integrated light. This is essentially the same procedure as for globular clusters, except we are projecting the temperature-luminosity points onto the temperature axis (weighted by luminosity). The only other catches are that
- Star formation might have occurred over time, rather than a single burst, so you have to assume some model for this, possibly with a few adjustable parameters;
- You have to assume an initial mass function - a distribution of masses for newly formed stars - which is often posited to be the same for all conditions (an assumption we hope is not too wrong).
On the other hand, if you just want to know "how long after the Big Bang was this galaxy in the state in which I currently see it?" then you really are in luck. If the galaxy is nearby enough to get a good spectrum, one can identify narrow spectral features that correspond to known transitions. The redshift $z$ is defined by $$ 1 + z = \frac{\lambda_\mathrm{observed}}{\lambda_\mathrm{emitted}}. $$ Assuming the redshift is dominated by cosmological expansion rather than the peculiar motion of us or the other galaxy, then the age of the universe when the light was emitted is given by $$ t_\mathrm{emitted} = t_0 - \frac{1}{H_0} \int_0^z \frac{1}{(1+z')\sqrt{\Omega_{\Lambda,0}+\Omega_{\mathrm{m},0}(1+z')^3}} \ \mathrm{d}z'. $$ The cosmological parameters $t_0$ (the current age of the universe), $H_0$, $\Omega_{\Lambda,0}$, and $\Omega_{\mathrm{m},0}$ are known from various sources, such as the CMB, the clustering of galaxies, or supernova surveys.
In cases of extremely distant or faint galaxies, spectra might not be an option. One can do a similar but rougher analysis using different photometric bands to try to reconstruct the redshift, but this depends on having some a priori knowledge of what the overall spectrum looks like.
One can thus get "ages" for individual galaxies. Plotting distributions of galaxy properties as functions of age shows a complicated history of how galaxies have changed over billions of years.
1 See for instance MESA, the core files of which consist of roughly $100{,}000$ lines of code.
2 There is easily a factor of $10^4$ between the main sequence lifetimes of the smallest versus the largest stars.
I'll add a few more options for getting the ages of stars, beyond the HR diagram technique mentioned in Chris White's answer.
If you can get a R=50,000 optical spectrum of a star with decent signal to noise ratio will quite easily give you the temperature (to 100K), surface gravity (to 0.1 dex) and metallicity (to 0.05 dex), plus a host of other elemental abundances (including Li) to precisions of about 0.1 dex.
Gravity: You can then plot the star in the log g (gravity) vs Teff plane and compare it with theoretical isochrones appropriate for the star's metallicity. This is the best way to estimate the age of a solar-type (or more massive) star, even if you don't have a distance and is the most-used method. How well this works and how unambiguously depends on the star's evolutionary stage. For stars like the Sun, you get an age precision of maybe 2 Gyr. For lower mass stars, well they hardly move whilst on the main sequence in 10Gyr, so you can't estimate the age like this unless you know the object is a pre-main sequence star. In young, pre-main sequence stars that are contracting towards the main sequence, then the gravity as measured from the spectrum is age-dependent.
Lithium Abundances: You can look at the Li abundance. Li abundance falls with age for solar-mass stars and below. This would work quite well for sun-like stars from ages of 0.3-2Gyr and for K-type stars from 0.1-0.5 Gyr and for M-dwarfs between 0.02-0.1 Gyr - i.e. in the range where Li starts to be depleted in the photosphere and where is is all gone. Typical precision might be a factor of two. A high Li abundance in K and M dwarfs usually indicates a pre main sequence status.
Rotation: If you can obtain a rotation rate from the broadening of the spectral lines or from rotational modulation, then you can use Gyrochronology, which works because the rotation rates of stars are time-dependent. Again, the applicability varies with mass, but in the opposite way to Li. M-dwarfs maintain fast rotation for longer than G-dwarfs. Of course you have the problem of uncertain inclination angle if all you have rotational broadening from a spectrum.
Magnetic Activity: That brings us to activity-age relations. You can measure the levels of chromospheric magnetic activity in the spectrum or coronal X-ray activity. Then combine this with empirical relationships between activity and age (e.g. Mamajek & Hillenbrand 2008). This can give you the age to a factor of two for stars older than a few hundred Myr. Its poorly calibrated for stars less massive than the Sun though. But in general a more active M-dwarf is likely to be younger than a less active M dwarf. It should certainly distinguish between a 2Gyr and 8Gyr M dwarf.
Kinematics: If you measure the line of sight velocity from your spectrum, this can give you at least a probabilistic idea of what stellar population the star belongs to. Higher velocities would tend to indicate an older star. This would work better if you had the proper motion (and preferably the distance too, roll on the Gaia results). If you have 3D kinematics for a young star you might be able to project its motion back in the Galactic potential and work out how long it has been travelling from it's birth-place. This has been done for a few objects (e.g. runaway OB stars) to work out how long they have been travelling 9which of course is a lower limit to their age).
Metallicity: In a probabilistic sense, low metallicity stars are older than high metallicity stars. If you were talking about stars as old as 8Gyr, these would be quite likely to have low metallicity.
Radioisotope dating: As an add-on I'll also mention radio-isotope dating. If you can measure the abundances of isotopes of U and Th with long half lives and then make some guess at their initial abundances using other r-process elements as a guide then you get an age estimate - "nucleocosmochronology". Currently, these are very inaccurate - factors of 2 differences for the same star depending on what methods you adopt.
In summary. If you are talking about G-dwarfs you can get ages to precisions of about 20% using log g and Teff from the spectrum. For M dwarfs, unless you are fortunate enough to be looking at a young PMS object with Li, then your precision is going to be a few Gyr at best for an individual object, though combining probabilistic estimates from activity, metallicity and kinematics simultaneously might narrow this a bit.
Read Soderblom (2013); Jeffries (2014); Soderblom et al. (2014).