The dynasty was named after Muhammad bin Abu Hafs a Berber from the Masmuda tribe of Morocco. He was appointed governor of Ifriqiya (present day Tunisia) by Muhammad an-Nasir, Caliph of the Almohad empire between 1198-1213. The Banu Hafs, were a powerful group amongst the Almohads; their ancestor is Omar Abu Hafs al-Hentati, a member of the council of ten and a close companion of Ibn Tumart. His original name was "Fesga Oumzal", which later changed to "Abu Hafs Omar ibn Yahya al-Hentati" (also known as "Omar Inti") since it was a tradition of Ibn Tumart to rename his close companions once they had adhered to his religious teachings.
In the 14th century the empire underwent a temporary decline. Although the Hafsids succeeded for a time in subjugating the empire of the Abdalwadids of Tlemcen, between 1347 and 1357 they were twice conquered by the Merinids of Morocco. The Abdalwadids however could not defeat the Bedouin; ultimately, the Hafsids were able to regain their empire. During the same period plague epidemics caused a considerable fall in population, further weakening the empire.
Under the Hafsids, commerce with Christian Europe grew significantly, however piracy against Christian shipping grew as well, particularly during the rule of Abd al-Aziz II (1394–1434). In 1429, the Hafsids attacked the island of Malta, and took 3000 slaves although they did not conquer the island. The profits were used for a great building programme and to support art and culture. However, piracy also provoked retaliation from Aragon and Venice, which several times attacked Tunisian coastal cities. Under Utman (1435–1488) the Hafsids reached their zenith, as the caravan trade through the Sahara and with Egypt was developed, as well as sea trade with Venice and Aragon. The Bedouins and the cities of the empire became largely independent, leaving the Hafsids in control of only Tunis and Constantine.
In the 16th century the Hafsids became increasingly caught up in the power struggle between Spain and the Ottoman Empire-supported Corsairs. The Ottomans conquered Tunis in 1534 (see Conquest of Tunis (1534)) and held it for one year, driving out the Hafsid ruler Muley Hassan. A year later the Holy Roman Emperor Charles V, Holy Roman Emperor seized Tunis, drove the Ottomans out and restored Muley Hassan as a Hapsburg tributary (see Conquest of Tunis (1535)). Due to the Ottoman threat, the Hafsids were vassals of Spain after 1535. The Ottomans again conquered Tunis in 1569 and held it for four years. Don Juan of Austria recaptured it in 1573. The Ottomans reconquered Tunis in 1574, and Muhammad IV, the last Caliph of the Hafsids, was brought to Constantinople and was subsequently executed due to his collaboration with Spain and the desire of the Ottoman Sultan to take the title of Caliph as he now controlled Mecca and Medina. The Hafsid lineage survived the Ottoman massacre by a branch of the family being taken to the Canary Island of Tenerife by the Spanish.