China has often come to the aid of Pakistan when it requires any diplomatic support in the world arena.
Why do China and Pakistan share a strong relationship?
Dispute of Johnson Line and Aksai Chin (1865): India and China both demands Aksai Chin to be the part of their territory.
McMohan Line (1914): China rejected some parts of McMohan line, especially the border between Tibet and Arunachal, which demarks the border between India and China proposed by British rulers.
Status of Tibet and Asylum of Dalai Lama (1959): India protested the occupation of Tibet by China. During the conflict between China and Tibet, India gave asylum to the fleeing Tibetan religious leader Dalai Lama who still lives in India.
Sino-Indian War (1962): Due to border disputes (McMahon Line) and the dispute about the possession of Aksai Chin, China fought a war with India and India lost the war miserably.
Sino-Pakistan Agreement (1963): Seeing India in conflict with China, Pakistan decided to establish a strong relationship by ceding those lands to China which are demanded by both China and India. Thereby, Pakistan ceded 5,180 sqkm of land which is part of Northern Kashmir and Ladakh and China ceded 1942 sqkm of land to Pakistan which resulted in the end of the land dispute between China and Pakistan once and for all. The land that Pakistan handed to China, is now disputed between India and China.
Permanent membership of China in the UNSC (1971): Pakistan was the broker behind the secret visit of Henry Kissinger to China during the 1970s, collected votes from the Arab world in favor of China, and thereby paved the way for China to become a permanent member of the UNSC.
American embargo on defense-products: USA sanctioned Pakistan during the early 80's for its nuclear program. The USA withheld both the downpayment money and the supply of 36 F-16 planes. China was sanctioned during the late 80's for the crackdown on civilian protestors in Tien-an-men square. These two sanctions opened the gate for defense cooperation between these two victims in those areas which were rare. For instance, aerospace, nuclear, and so on.
Neo Indo-US alliance: Between India and China, the USA preferred India to form an alliance with. There are several reasons behind that,
This alliance has made the life of Pakistan very difficult. Because,
This has encouraged the Sino-Pak alliance, and more.
No one is likely to give a firm answer since China doesn't normally publicize its internal deliberations. But a few educated cases would note that (remembering that India and Pakistan are enemies):
China has a poor relationship with India. They have an ongoing border dispute that China has recently been pushing.
China has a poor relationship with India. China doesn't like how India has supported the Dalai Lama
When you say "other countries have Pakistan cornered" I'm not sure what you mean, but China tends to resist strongly the idea that countries should be governed by international norms particularly when those norms could be interpreted as interfering in the internal affairs of a nation.
It just happened. Pakistan behaved like a friend even before China was big deal. Its unwitting, friendly behaviour was rather spontaneous and unconditional without any calculation. When China was weak and humble and was the pariah of the world, Pakistan was the only fool who lent China a hand. For a long time, Pakistan was the only conduit through which China communicated with the outside world. When Sino-American relation began to thaw, it was Pakistan who funnelled friends China's way and eventually promoted peace between two arch-rivals.
In a sea of people, how do you tell who is your friend?
In plain English, Pakistan, in the Chinese eye, is a proverbial friend.