The term "the Big Apple," as a reference to New York City, appears to have two separate origins. Both origins date to the early 20th century, and both originated in slang.
The first theory of the origin of "the Big Apple" comes from the world of horse racing. Around the turn of the 20th century, horses were raced for a cash prize that was colloquially known as "the apple." The biggest "apples" in the racing world were the prizes given out for races in New York. Jockeys and owners alike began referring to New York's competitions as "the Big Apple."
Another possible path for the phrase may have come from the world of itinerant jazz musicians. Around the same time that "the Big Apple" was becoming popular among fans of horse racing, the adage "there are many apples on a tree, but there's only one big one" came to be applied to venues in New York City. Unlike performances in smaller venues around the country, musicians came to view getting a career in New York as a sign of professional success.
Whatever the exact origin of the phrase, New York City adopted "the Big Apple" as an official designation in 1971. This move cemented the phrase as a recognized term for the city and helped to rehabilitate New York's reputation and encourage tourism.