Morocco has the best train in Africa. Tangier to Casablanca has high-speed train (TGV) service, and Casablanca has a shuttle train (French: Train Navette Rapide, "Fast Shuttle Train"in English, more commonly known as the "TNR" or "Aouita") to Kenitra (via Rabat), El Jadida, Settat and Mohammed V International Airport (it is renamed Al Bidaoui).
The first way is to buy a ticket at the train station.
The second way is buy ticket at on the official website, you'll need google translate if you don't understand French.
The third way is book by phone, When you're in Morocco, you can also book by phone on 0890 20 30 40, but this number is not accessible from overseas.
But one thing I must mention everyone is Supratours, the bus company, a subsidiary of Moroccan railways. You can buy combined train+bus tickets from any Moroccan railway station if your destination has no rail service.
Fares are pretty cheap by European standards.
Children aged 0-3 travel free, children aged 4-10 travel at half fare, children aged 11 and over pay the adult fare.