Malam is a Hausa word which is a corruption of the Arabic Mu'allim which means teacher.
A malam is primarily a teacher. Usually teaching the Islamic curriculum in a madrassah (Arabic for school). However, in Hausaland, malam is also a title which is similar to mister. Additionally, given that malams are (supposed to be) knowledgeable, some of them dabble in the supernatural. Some provide supernatural protection in the form of charms, potions and incantations while others do nefarious things. They provide love potions, potions and charms to make people fail or fall out of love or die or become poor.
In Yorubaland (southwestern Nigeria), the term for an Islamic teacher is alfa. I could not find its relationship to any Arabic word. However, it is possible that it is a contraction of alu ifa which means beater (of drums) for Ifa. Ifa is the traditional Yoruba religion and drums are important in it. It is possible the drummers were priests and since Islam did not destroy Yoruba culture as thoroughly as it did that of Hausas, the traditional culture retained enough vigor to be assimilated into the Islamic one.