With the exception of transit passengers (see below), you must have a visa (签证) to visit mainland China. In most cases, this should be obtained from a Chinese embassy or consulate well before departure. Citizens from most Western countries do not need visas to visit Hong Kong and Macau. Visitors from most western countries can stay in Hong Kong with free visa for 7 to 90 day. The time duration should depend on which country you are from.
Chinese visa applications can, in some jurisdictions, for some types of passport holders, be applied for online.