We consider fog computing scenarios where data generated by a set of IoT applications need to be processed locally by a set of fog nodes, belonging to distinct Fog Infrastructure Providers (FIPs) sharing the same co-location facility, with the aim of increasing their profits. This is a challenging goal as it requires reducing costs and meeting QoS targets despite time-varying workloads. We argue that these FIPs may find it profitable to cooperate, by mutually sharing their workload and resources, and we show (by using a game-theoretical framework) that this is indeed the case when stable coalitions can be formed. Based on these results, in this paper we present (1) a mathematical model for maximizing the profit obtained for allocating IoT applications to a group of FIPs, and (2) a coalition formation algorithm that allows each FIP to decide with whom to cooperate so as to increment its profits. The efficacy of the devised algorithm is assessed by means of an experimental evaluation taking into account different workload intensities. The results from these experiments show the capability of the proposed algorithm to form coalitions of FIPs that are profitable and stable in all the scenarios we take into consideration.