Efficient load balancing in fog nodes is essential for optimal resource utilization in fog computing environments. Despite advancements, existing methods lack efficiency, prompting the introduction of an optimization-based dynamic allocation approach in this paper. This paper introduces an approach, Task Classification and Buffering (TCB), designed for effective task management by categorizing tasks and optimizing buffer utilization. Simultaneously, Task Offloading and Optimal Resources Allocation (TOORA), empowered by the Bat Optimization Algorithm (BOA), ensures optimal resource allocation for offloaded tasks, thereby boosting overall system performance. The proposed methodology is evaluated using performance metrics, including Energy Consumption (6.00 W), Cost ($14.53), Make-span (0.32 ms), and Latency(1000s), demonstrating its efficacy. Comparative analysis with existing methods like Dynamic Energy Efficient Resource Allocation (DEER), accelerated Particle Swarm Optimization (APSO) algorithm, and Cat Swarm Optimization (CSO) with Support Vector Machines (SVM) underscores the proposed TCB and TOORA approach in achieving optimal load balancing in fog computing environments.