They are both correct.
I am a twenty-year-old student:
- "20" and "year" are linked by the hyphen to create a single term ("20-year") that modifies "old."
- "20-year" and "old" are linked by the hyphen to create a single term ("20-year-old") that modifies "student."
I am twenty years old:
- "old" is a predicate adjective for "I." (as in "I am old.") As a single-word adjective for "I," it is not joined with a hyphen to the previous adjective, "years."
- "years" modifies "old," and "20" modifies "year". "20" and "years" are not acting as a single adjective for "old," so they, too, do not need a hyphen.
The easy understanding:
If the adjective phrase serves as a single modifier before the noun (student, in the second sentence), it needs a hyphen (or hyphens, as in this case). Adjective phrases have no plural forms on the words forming them (the word year you asked). When you use a noun as an adjective (year-old), it must be singular. That's why there is no -s there.
If it is after the noun, it doesn't need a hyphen (or hyphens). When the same words (years) are used not as an single adjective, which means they modify the noun separately, the noun is plural. That's why there is an -s there (unless it's She is 1 year old).
- The ceiling is ten feet high.
- The room has a ten-foot ceiling.