Generally, you will have need to have held an existing ticket before getting onboard. The only exception is if you board at halts, or small, unstaffed stations, and you were unable to use a ticketing facility.
In general, tickets bought onboard will require you to pay a general handling fee (¥2.—); however, this is waived you boarded from halts, or small, unstaffed stations, and were unable to get tickets before travelling.
As a rule, tickets bought onboard are offered only as far as the final station served by the train — so if you are onboard a train from Beijing to Shanghai, you cannot buy onboard tickets for continued travel to Hangzhou or Shenzhen. However, you can extend your trip to the final destination served by the train, in case your ticket “ends” at an earlier stop.
You may need to present ID as personal ticketing is being practiced on an increasing number of Chinese trains as well.
In addition to extending your trip, you can also upgrade to a higher class of travel for part or the entirety of your journey.
Please note: During peak travel periods, especially over Spring Festival, there might not be an option to extend your journey — for obvious reasons, as millions will be travelling then!