This study presents a comprehensive framework for optimizing 5G network slices using metaheuristic algorithms, focusing on Enhanced Mobile Broadband (eMBB), Ultra-Reliable Low-Latency Communications (URLLC), and massive Machine Type Communications (mMTC) scenarios. The initial setup involves a MATLAB-based 5G New Radio (NR) Physical Downlink Shared Channel (PDSCH) simulation and OpenAir-Interface (OAI) 5G network testbed, utilizing Ubuntu 22.04 Long Term Support (LTS), MicroStack, Open-Source MANO (OSM), and k3OS to create a versatile testing environment. Key network parameters are identified for optimization, including power control settings, signal-to-noise ratio targets, and resource block allocation, to address the unique requirements of different 5G use cases. Metaheuristic algorithms, specifically the Whale Optimization Algorithm (WOA), Particle Swarm Optimization (PSO), and Genetic Algorithm (GA), are employed to optimize these parameters. The algorithms are assessed based on their ability to enhance throughput, reduce latency, and minimize jitter within the network slices and the MATLAB simulation model. Each algorithm’s performance is evaluated through iterative testing, with improvements measured against established pre-optimization benchmarks. The results demonstrate significant enhancements in network performance post-optimization. For eMBB, the GA shows the most substantial increase in throughput, while PSO is most effective in reducing latency for URLLC applications. In mMTC scenarios, GA achieves the most notable reduction in jitter, illustrating the potential of metaheuristic algorithms in fine-tuning 5G networks to meet diverse service requirements. The study concludes that the strategic application of these algorithms can significantly improve the efficiency and reliability of 5G network slices, offering a scalable approach to managing the complex dynamics of next-generation wireless networks.