The increase in data rate demands has driven the development of Cell-Free massive Multiple-Input Multiple-Output (CFmMIMO) technology in 6G Heterogeneous Networks (HetNets). This paper proposes an integration of Unmanned Aerial Vehicles (UAVs) and Device-to-Device (D2D) communication in CFmMIMO, aiming to enhance network capacity, coverage, and service delivery in next-generation wireless networks. The paper investigates uplink transmission in a network where Access Points (APs) have imperfect channel state information. It considers Cell-free User Equipment (CUE), UAVs, and D2D pairs, deriving closed-form uplink achievable rates for each user type. Moreover, two optimization problems are formulated to enhance user data rates: one focuses on maximizing the sum data rate through pilot assignment, while the other aims to achieve weighted max–min power control through power allocation. Furthermore, to address these problem, we propose a Modified Whale Optimization Algorithm (MWOA) and a novel Pilot Assignment based on Whale Optimization Algorithm (PAWOA). Performance studies demonstrate significant improvements over state-of-the-art algorithms.