Hello, one time we simply took the train/metro from the airport to the Atocha train station and bought our tickets to Pamplona. Easy peasy.
On our second Camino, we bought our tickets online and printed them out at home. We found that purchasing the train ticket from the Atocha station to Pamplona online included the train/metro section from the airport to the Atocha station. You have to stop at the Renfe automatic ticket machine in the airport (after you have exited the secure zone) on your way to the airports' train/metro station and enter a code from your purchase. The machine then prints off your ticket from the airport to the Atocha station. The Renfe folks at the airport were very helpful and friendly to us lost, confused and sleep deprived pilgrims.
On our third Camino, we did not buy online (this time from Lisbon to Porto) since we could not print the tickets (they had to mail them and there was not enough time), so we decided to simply purchase once we got to the train station. We were not able to get the train we wanted, it was sold out, and it cost us three hours of time (combination of later departure and slower train) we lost to walk from the Cathedral in Porto to Matosinhos.
Having taken the bus from Santiago to Porto after our Portuguese Camino, take the train. Faster and generally more comfortable, you can walk around, get something to eat or drink, etc.