To answer the when question we need some very broad historical context. To generalize, what we consider "science" has its roots in ancient Greek scholarship. The conventional narrative of the history of science traces the history of Greek scholarship and who was using it.
This narrative begins with Aristotle and Alexander in the early classical age. Alexander's conquests in the 4th century BCE initiated the Hellenization of much of the Middle East. Greek culture spread throughout the region, as far as Central and South Asia. The expansion of the Roman Empire further solidified connections across the Mediterranean Sea, and cross-cultural interactions continued to flourish.
As Western Rome went into decline, Eastern Rome (the Byzantine Empire) increasingly began to use Greek, and played a role in preserving Greek scholarship. Then around 600 CE, the Islamic Empires arose--The Umayyad Dynasty swiftly united the Middle East and Northern Africa under a single theocratic empire. Through control of previously Hellenized areas, and interactions with the Byzantine empire, Muslim scholars increasingly began to study the ancient Greek scholarship. After the Abbasid Dynasty wrested control from the Umayyads, the Golden Age began, peaking in the 11th and 12th centuries.
The Abbasids officially sponsored the type of scholarship in question, mostly famously in the Baghdad House of Wisdom. During this period the "translation movements" led to the mass translation of Greek works into Arabic. These scholars were primarily interested in using Greek knowledge and techniques to answer religious questions. Algebra was primarily developed to more easily follow Islamic inheritance law. Advancements in trigonometry and astronomy were used for determining the qibla (the direction to Mecca). In the latter example, advancements in knowledge led to fascinating religious dilemmas, as many mosques were discovered to be oriented in the wrong direction. Some of these were destroyed and re-built, but other religious scholars argued that those mosques were built on valid religious truth and should be preserved. This foreshadowed philosophical debates that help to answer your question.
Then, as the Western European kingdoms began to stabilize in the same period, interactions across the Mediterranean and the Iberian Peninsula began to resume. Trade, migrations, the Reconquista, and the Crusades brought Greek scholarship to western Europe as Christian education was beginning to flourish. Scholasticism firmly established Aristotle as an authority in European intellectual circles. This was followed by a series of transformations in Europe that set the conditions for the development of science as we know it. The Renaissance began to slowly bubble up along with Humanism. The discovery of the new world, the Renaissance, and the Scientific Revolution threw the Europeans view of their social and physical world for a loop, and began the European march towards modern science. So from this story, it seems like the transition you're asking about probably happened sometime between the 13th century and the 17th century. The path towards the Scientific Revolution is well-studied, and your question seems to focus mainly on why science declined in the Islamic world, so let's shift our focus back there. Because we now have when the shift happened, and a bit of why Europe began such scholarship, but doesn't tell us why the Islamic world stopped.
There are two fundamental parts of that story that become important here: the official sponsorship of the Abbasid Dynasty, and the fact that Islamic science was so specifically rooted in religion. That second piece of the puzzle leads to conflicting theories about the real answer to your question.
The conventional narrative holds that religious and philosophical conflicts in the Islamic world led to the abandonment of science starting in the 12th century. This is the type of narrative you will hear very commonly, even from people like Neil DeGrasse Tyson. According to this story, religious traditionalists began to argue against progressive scholarship and won out, leading to a massive trend towards religious conservatism and an anti-scientific mindset that continues to this day. The blame is primarily put on the 12th century religious philosopher al-Ghazali. But there is some reason to doubt this narrative.
The primary text pointed to as proof of al-Ghazali's attack on the scientific world is The Incoherence of the Philosophers. In this text, he crafts a religious critique of Hellenistic philosophy and Aristotle in particular. He particularly attempts to deconstruct Aristotle's work on causality, something that would seem to be in direct conflict with science. He constructs a lengthy argument that defends the primacy of religious interpretation of nature, and sets himself squarely in opposition to the Hellenistic philosophers.
But diving deeper into his works, he doesn't seem to be as radical as the conventional narrative would make him out to be. At several points, he defends geometric demonstrations. He warns that if one casts doubt on them, people will not begin to doubt geometry, they will just doubt religion. He seems to be primarily attacking Greek philosophy, not necessarily the foundations of empiricism itself. Abandoning Greek philosophy was the hallmark of Europe's scientific revolution. And al-Ghazali's critique of causal relationships is strikingly similar to doubts expressed by David Hume centuries later.
In addition, al-Ghazali did not stand alone, nor were his ideas particularly dominant during the transitional period in question. He was part of a discourse that continued for a long period of time. Ibn Rushd (Averroes), one of the most prolific Muslim scholars and scientists, made direct commentaries on al-Ghazali after his death. Historian George Saliba offers a list of Muslim scientists from 1205 to 1550. None of these figures would be likely to disagree that religion was an important foundation of their scientific work. And almost all of them would modify or abandon the Greek works in some way. They used them as a starting point for original and productive scientific works of their own.
This means that we need to look at other reasons for the decline of science in the Islamic world. One plausible explanation is the political changes that occurred after the 12th century. With the decline of the Abbasids, the upheavals of the Crusades, and the arrival of the Mongols, the areas of scholarship were disrupted time and time again. The loss of official sponsorship and instability that ensued could provide one explanation. But as we saw, scholarship did indeed continue despite these changes, even if it slowed.
When you look at the situation today, however, we do see evidence of massive decline. In 2003, the OECD found that a pool of 46 Muslim countries produced only 1.17 percent of scientific papers in the world. India and Spain each produced a slightly larger percentage by themselves. So if we only see a gradual decline from 1205-1550, but then we see dramatic decline and a lack of any sort of recovery today--what happened after 1550? The answer may be colonization.
In many cases during the 18th and 19th century, science became entwined with the European "civilizing mission." Rather than being participants in science, many people in colonized countries became scientific subjects. In many cases, particularly in North Africa, colonized people were kept from fully participating in institutions of the colonizers, while their native institutions were left to slowly decay. Such an experience is described vividly by Albert Memmi, a writer who lived in French Tunisia. And so as science was set up in opposition to them, any scientific culture that remained would be lost.
As colonization became more intense, and the Ottoman Empire began its decline, the power structures necessary to support scientific culture in the Islamic world collapsed. The dysfunctional power structures that replaced them left no room for a scientific culture to recover. One of the few social institutions tenacious enough to survive the colonial experience was religious conservatism. And since decolonization, religious conservatism has taken control of much of the power structures in the Islamic world. But left behind is the scientific culture that was lost along the way. And because that scientific culture is now so strongly associated with the west and colonization, it seems unlikely that power structures will start to promote a truly scientific culture. Instead what we seem to be seeing recently is the promotion of a purely pragmatic engineering culture, meant to support and protect the existing power structures.
In many ways, the transition you are asking about is not something that was isolated to the Medieval or Early Modern period, but is a long complex process, an important part of which we are living through right now.
There have been some Muslim scholars throughout the 20th and 21st century making arguments similar to those of the 12th century scientists. Muhammad Abduh in particular argued that scientific investigation is an important part of religious practice, as did Ibn Rushd centuries earlier. It will be interesting to see how this discourse continues.
Sources:
Saliba, George, Islamic Science and the Making of the European Renaissance
Haq, Syed Nomanul Science in Islam
Dallal, Ahmad, Islam, Science, and the Challenge of History
Al-Ghazali, Micael Marmura, trans. The Incoherence of the Philosophers
Averroes, George Hourani, trans. On the Harmony of Religion and Philosophy
Hoodbhoy, Pervez Amirali Science and the Islamic World--The Quest for Rapprochement
Memmi, Albert, The Colonizer and the Colonized
Abduh, Muhhamad, Ishaw Musa'ad and Kenneth Cragg, trans. The Theology of Unity
Much of this response is adapted from the capstone research project I did for my undergrad degree years ago.