Mandarin is the official standard language of China. It is based on the Beijing dialect. Most Chinese people can speak Mandarin, since everyone must learn to speak it at school. In big cities like Beijing and Shanghai where people come from different parts of China, they usually talk to each other in Mandarin in order to be understood.
Cantonese is just a dialect of Chinese. It is mainly spoken in the province of Guangdong, and the cities of Hongkong and Macao.
Spoken Mandarin and Cantonese have different pronunciations and tones, but the grammar and the written forms are the same.
If you hear people speak a dialect in China, you can always strike up a conversation with them in Mandarin. Mandarin is indeed the language of communication in China.