The claim comes from the 'Chronica Majora' by Matthew Paris which forms part of the The Saint Alban’s Chronicles. Paris was an English Benedictine Monk who wrote about the alleged event some twenty years after it was meant to have happened.
In his version of events King John visited Muhammad al-Nasir, the Caliph of the Almohad Caliphate which spanned a large swathe of the Mediterranean coast and offered to convert himself and the English to Islam.
Matthew Paris is not considered a reliable historical source by any stretch and certainly not in regards to King John.
This isn't unusual for chroniclers of that era, the study and recording of history didn't have the academic rigour that it does today. Rather it lavishly incorporated religious beliefs, national mythologies and political propaganda. The practice of compiling was also common, where parts of previous chronicles deemed favourable would be inserted; the Medieval version of copypasta.
The propaganda message here is 'King John is worse than the enemies of Christendom because he wanted to coerce his people to leave their religion and convert, something which even Islam forbids a King to do'
To put this in to some context, Chronica Majoa was written more than a century before the first stirrings of the English pre-reformation and so the Religious coercion which would come later was not yet an issue.
Because there is such a lack of reliable contemporary accounts for this period Paris is often used as a source by modern historians, but crucially not where his claims aren't corroborated.
The historical accuracy of events relayed in his chronicles and those of others like him are judged through the quantity and consistency of the accounts. As well as whether other evidence available backs them up. The chronicles blended fact and fiction and so it's the task of modern Historians to try to separate that mixture.
If many chroniclers have the same basic story - it's more likely to be true. The King John story falls down at this hurdle. There's no account of these events anywhere else in the contemporary historical record, the Chronica Majora is the only source.
Furthermore, Paris is known to have had reason to want to paint an unfavourable picture of John. His Abbey was put under secular rule by him during the interdiction of 1208-1214 and thus, for this reason and others, he bore him a grudge.
His account of King John's reign ends with the lines
“With John’s foul deeds England’s whole realm is stinking - Hell itself is defiled by the foul presence of John”
The Wikipedia talk page on the King John article has some discussion on whether this story ought to be included given the scant evidence for its veracity and the strong evidence of Paris' bias.
To add to /u/intangible-tangerine's excellent explanation, and not to in any way detract from it, I'd like to mention W.L. Warren's biography of King John, which opens with a discussion of the various contemporary and near-contemporary accounts of John's reign and why they are so unreliable, especially when it comes to such defamatory tales as the one OP learned about today.
Many details of John's perfidy were complete fabrications by Roger of Wendover, a monk who was the official historiographer at St. Albans. Wendover did not begin writing about King John until about ten years after John's death. His chronicle contains considerably more deleterious detail than those who were writing contemporaneously with the events they recorded. Wendover took extreme liberties with fact when it came to chronicling John's acts, but his accounts were so detailed - complete with recorded conversations - that people who did not examine those details closely for things like consistency (not to mention whether the acts were chronologically or physically even possible) believed them. Some of the anecdotes were really wild.
Matthew Paris took over as historiographer at St. Albans in 1235 - now a full generation after John's death. Since John's reign began in 1189 1199 when Richard I died, Paris was "recording" events in lurid detail nearly 20-50 years after they allegedly happened, mostly based on Wendover's fictitious anecdotes.
According to Warren:
Paris was no punctilious historian either, and wrote with strong partisan prejudices. He hated foreigners, lawyers, theologians, civil servants, papal 'interference', and any form of taxation. He was violently nationalist and pro-baronial, and did not hesitate to write tendentiously. But he was a superlative literary artist. He worked wonders with Wendover's text, polishing it here, propping it up with an epigram there, putting speeches into silent mouths. 'By God's feet, now at last am I king and lord of England,' he makes John say on hearing of the death of his chief official. He seems to have been rather pleased with this invention, for on writing an abridged version of his chronicle he transferred it to the death of another and greater royal official where, on second thoughts, it seemed more appropriate.
Warren recounted the tale of John offering England to an emir in Northern Africa, quoting at length from an 1872 translation of Paris's Chronica Majora.
The portrait of John that emerges from Paris is, in consequence, even further removed from reality than that in Wendover, but it is eminently more readable. One of the new passages that Paris introduced is particularly arresting. He would have us believe that in 1213, when John was hard pressed by enemies both at home and abroad, he sought to gain a protector by offering to make England a tributary to the Moslem ruler in North Africa, and to abandon Christianity, 'which he considered false', and to embrace Islam. Paris, as a born raconteur, fills out his story of the experience of John's envoys with much picturesque detail, so that even so essentially improbably a tale takes on some semblance of authenticity. When the envoys had penetrated innumerable and terrifying guards to reach the emir's presence, he says, they found him reading a copy of the epistles of St. Paul in Greek. He confessed that he was profoundly impressed by Paul, but could not stomach his desertion of the faith of his fathers. Indeed, the emir felt so strongly about apostasy that the mission was hopeless from the start. Nonetheless he was interested to hear what the envoys had to say. Speaking to their brief, they dwelt glowingly on England's prosperity and happy condition under an illustrious monarch. The overdid it: instead of seeing England as a desirable acquisition, the emir only became incredulous that John should propose submitting it to the rule of another.
'The ambassadors were retiring discomfited,' writes Paris, 'when the emir became aware of the third member of the party, Robert the clergyman, who was small and swarthy with a misshapen hand (for two fingers stuck together) and a face like a Jew. He called for him to be brought back for he had noticed that while the others had done the talking Robert had remained silent and detached, and it occurred to him that so ill-favored a person would not be sent on so delicate a mission unless he was thought to be particularly shrewd and intelligent. Moreover, he realized from the man's tonsure that he was a member of the clergy. So, keeping him back and dismissing the others, the emir had a long private talk with him, which Robert afterwards related to his friends. The emir wanted to know if King John were a man of sound morals, if he were virile and bore lusty sons, adding that if Robert did not reply truthfully he would never believe a Christian again, certainly not a tonsured one. So Robert, promising on his word as a Christian to tell the truth, was obliged to admit that John was a tyrant not a king, a destroyer instead of a governor, crushing his own people and favouring aliens, a lion to his subjects but a lamb to foreigners and rebels. He had lost the duchy of Normandy and many other territories through sloth, and was actually keen to lose his kingdom of England or to ruin it. He was an insatiable extorter of money; he invaded and destroyed his subjects' property; and he had bred no worthy children but only such as took after their father. He detested his wife and she him. She was an incestuous and depraved woman, and so notoriously guilty of adultery that the king had given orders that her lovers were to be seized and throttled on her bed. He himself was envious of many of his barons and kinsfolk, and seduced their more attractive daughters and sisters. As for Christianity he was unstable and unfaithful. When the emir heard this, he no longer merely despised John; he loathed him.'
According to Warren and to his cited sources, Paris apparently realized that he had embellished John's story a bit much, and tried to tone it down in later accounts of his reign, even grudgingly saying some positive things about him. However, Paris's credibility was so strained by that point that no one believed those tales, either.
Apparently, despite not being believed by any serious historians, the stories were just too good not to keep telling.