In this paper we deal with the problem of making a set of Fog Infrastructure Providers (FIPs) increase their profits when allocating their resources to process the data generated by IoT applications that need to meet specific QoS targets in face of time-varying workloads. We show that if FIPs cooperate among them, by mutually sharing their workloads and resources, then each one of them can improve its net profit. By using a game-theoretic framework, we study the problem of forming stable coalitions among FIPs. Furthermore, we propose a mathematical optimization model to allocate IoT applications to a set of FIPs, in order to reduce costs and, at the same time, to meet the corresponding QoS targets. Based on this, we propose an algorithm, based on cooperative game theory, that enables each FIP to decide with whom to cooperate in order to increase its profits. The effectiveness of the proposed algorithm is demonstrated through an experimental evaluation considering various workload intensities. The results we obtain from these experiments show the ability of our algorithm to form coalitions of FIPs that are stable and profitable in all the scenarios we consider.