Madrid-Barajas Airport is fairly close to the city for a major city. It is only about 7 miles. There are several ways to get into the city.
You can take the metro and use line 8. The cost will be €4.50-€5 when using the metro.
There is a bus that runs 24 hours a day to get into the city. The Airport Express bus. This is a great option for late arriving flights. The cost is €5.
Of course, taking a taxi is always an option, but at a cost of ~€25, it would be my last option.
There is a light rail (metro ligero) as well that runs from Terminal 4 into the city. I believe it is associated with the metro system, but this is a light rail train rather than a metro train. Tickets are €4.50.
We took the Renfe train, which is a Spanish railway company. The price of the ticket includes a ride on the Renfe train, as well as a metro ticket if your final destination is not one of their stops. Tickets are only €2.60 from the airport, and must be used within 2 hours of purchase.
The station is on the T4 terminal floor, and you will look for the Aeropuerto T4 sign, heading in the Principe Pio direction.
The train ride does have some luggage racks, but since we were traveling lightly we decided to hold onto our bags. This may be the most convenient way to get into the city, but if you have a lot of luggage, this might not be your top choice if you have to switch trains.