A major challenge for adaptive agents is achieving behavioral flexibility without compromising stability-particularly in goal-directed learning within uncertain environments. Agents must adjust as goals shift while maintaining resilience against noisy signals, necessitating the delicate tradeoff: balancing flexibility for goal pursuit with stability for preventing erratic behavior. To investigate how the brain navigates this dilemma, we combined model simulations with behavioral and fMRI data collected during a goal-directed learning task under varying levels of uncertainty. Our simulations revealed that model-free learning struggles with the flexibility-stability trade-off, whereas model-based learning allows for flexible goal pursuit with varying degrees of stability. Interestingly, human participants displayed both stable and flexible goal-directed behavior. The fMRI data uncovered the underlying mechanism: goals and uncertainty are represented as factorized embeddings in the lateral prefrontal and orbitofrontal cortex. Notably, the neural separability of goals and their resilience to uncertainty in these regions correlated with participants' behavioral flexibility and stability.