The symbol was adopted by the Ottoman Empire as a secular and not a religious symbol after the conquest of Asia Minor. There are multiple theories about its origins. One suggests that the crescent was a preexisting Turkic symbol, but that the combination of the star and crescent is not:
It seems possible, though not certain, that after the conquest Mehmed took over the crescent and star as an emblem of sovereignty from the Byzantines. The half-moon alone on a blood red flag, allegedly conferred on the Janissaries by Emir Orhan, was much older, as is demonstrated by numerous references to it dating from before 1453. But since these flags lack the star, which along with the half-moon is to be found on Sassanid and Byzantine municipal coins, it may be regarded as an innovation of Mehmed. It seems certain in the interior of Asia tribes of Turkish nomads had been using the half-moon alone as an emblem for some time past, but it is equally certain that crescent and star together are attested only for a much later period. There is good reason to believe that old Turkish and Byzantine traditions were combined in the emblem of the Ottoman and, much later, present-day Republican Turkish sovereignty.
The star was definitely linked with the authority of the Byzantine Emperor and his divine rule, as it appears on many Byzantine coins from the 6th-14th centuries, usually behind the image of the Emperor. Some theories point to it being one of the Stars of the Magi, while others attribute it to Constantine’s vision. Whatever the case, it was a symbol of power and authority and divine inspiration. But the crescent was the symbol of the Imperial province of Thrace, and of Constantinople itself, so, as noted, the Ottoman banner seems to have been borrowed from the existing symbology of the Byzantine Empire, probably to legitimize its claim in the early years. This is similar to the earlier Seljuq adoption of the title of Rum, meaning “Rome”, which was designed to support their legitimacy in historically Roman Anatolia.
The Crescent became linked with Islam because the Ottoman Empire was the largest and most powerful Islamic state for several centuries, up until the 20th century. The flags of Pakistan and Malaysia were created when the symbol of the star and crescent had been firmly embedded in the popular conscious as the symbol of Islam. Religious fundamentalist movements avoid the star and crescent symbol precisely because of its non-Islamic origins. They opt instead for flags of a single color, like those used by early Muslims, sometimes with calligraphy of the Shahada, the Muslim creed. The flags of Saudi Arabia and Hamas follow this principle.