If you are flying on Iberia or Oneworld, you will most likely come into T4/T4S.
You can take the Cercanias (commuter) train from there to Chamartin. If you
have or purchase the ticket to Valencia before you leave the airport, the
commuter train is included, and it is only 15 minutes train ride. So it is most
likely faster and definitely cheaper than a taxi. If you take the Metro, you
will have to change lines to get to Chamartin.
The Metro and Cercanias trains are in the same area of the airport. There are
machines for ticket purchases as well as staff to assist.
I looked and train tickets from Chamartin to Valencia tomorrow are like $15. So
I wouldn't worry about buying tickets in advance.