How do we know a star's age based on its spectrum? A star's nearby environment may give clue to its age.

Different stellar type have different spectral features. If we just have its spectrum, how do we know its age?

For example, a star may be born with a M3-type(I know it is more difficult for M-type compared with OBAFGK) and stay here for a long time.
How do we know it was born with a M3-type? 
How do we know its age? 

It is better to talk about different stellar types and give reference papers.
There are two separate issues when you talk about a star's age.  Are you talking about its absolute age, in years, or its age in terms of how far along is it within its own evolution?  The difference is, massive stars go through their evolutionary phases very quickly, and low-mass stars very slowly, so essentially all very low-mass stars are older in years than essentially every very high-mass star.   Also, you can know more than the "spectral type" from a spectrum, you can also know the "luminosity class"-- that's the roman numeral that tells you if the star is, for example, a main-sequence dwarf or not.  So if you pick an M spectral type main-sequence star, and an O spectral type main-sequence star, as two stars at random, it is extremely likely that the M star is much older than the O star, and you can know that purely from the spectral type and luminosity class.  But if you want to know the age of the M star, or the O star, in years within their evolutionary stages, then it gets more difficult because there is rather little main-sequence evolution in the spectrum.  There is some evolution though, especially late in the main sequence, so you could tell its age if there is evidence of main-sequence evolution in the spectrum (for one thing, the luminosity rises with age, within a spectral type, but that's pretty hard to tell from the spectrum).  If the star is both low-mass and early in its main-sequence evolution, the best way to tell its age is to look at the rotational broadening of its lines, as low-mass stars spin down rapidly as they age (due to strong magnetic fields and weak winds).

Another good way to tell the age of stars is if they are in clusters that all formed together, you can look at what is called the "main sequence turnoff", which basically means you look for the lowest mass star that has gone supernova, and you know how long that takes for that mass of star.  You tell this from the spectrum because the turnoff is in the "Hertsprung-Russell" diagram, which you make from the spectra, but it requires a lot of stars in the cluster, so it's not really a property of a single spectrum.

So the bottom line is, you need a suite of different techniques for assessing the ages of stars, it is not always easy or straightforward but you can take each case individually and look for ways to infer age.  Much easier is determining which evolutionary class a star is in, because stars change dramatically in one phase or another, and the spectrum alone can tell you if you have a dwarf or a giant, etc.  (Basically, the lines get much narrower as a star puffs out in radius, because the pressure drops.)