5G-based mobile ad hoc networks with cloud are a new paradigm that combines several real-world applications. Routing and security are current issues in MANETs. Security is highly important in MANET and cloud environments for preventing harmful attacks. Therefore, a trusted environment is required for a MANET with cloud-based 5G communications. In this paper, we propose a new framework called trust-aware intrusion detection and prevention system (TA-IDPS) for protecting the network from adversaries. TA-IDPS consists of a MANET, a cloudlet, and a cloud service layer. Initially, we register and authenticate mobile nodes using an ultra-lightweight symmetric cryptographic technique, which is highly suitable for resource-constrained environments. In MANETs, high energy consumption, scalability, and authentication are important issues, which are addressed by the proposed moth flame optimization algorithm. If the cluster head (CH) receives data packets from a source node, they are classified as normal, malicious, and suspicious using a deep belief network. Intra-cluster routing is implemented by an adaptive Bayesian estimator using next-best forwarder selection. In the cloudlet layer, cloudlets are used to aggregate packets from the CH and verify their legitimacy so that they can be forwarded to the cloud service layer. Each cloudlet is provided with a peek monitor for classifying suspicious packets as malicious and normal using Awads information entropy. Experiments are conducted using NS3.26. The performance of the proposed TA-IDPS and previous methods is analyzed using widely used metrics. The evaluation results demonstrated that the proposed TA-IDPS system outperformed the previous methods in terms of all metrics.