The song in question is an old jazz song called "Sweet Georgia Brown", in this case a version by Brother Bones. The Harlem Globetrotters, as far as I know, are the ones who first introduced the song into the basketball world.
They would play Brother Bones' version of the song during their warm-ups, and it became associated with their whimsical style of play. When they began producing television commercials to advertise their games, they used the song because people already recognized it as "the Globetrotter's theme".
Later use of the song for other basketball-related media is just playing on it's poplarity as the Globetrotter's music.