Normally scope is fixed by the source text, so each name can be resolved before the code runs. Two features break that, and both only fully work in sloppy code.
Direct eval (indirect eval is a separate question) runs the string in the caller's environments. PerformEval gives the eval code a fresh declarative record for its let, const and class, so those never escape. Its var and function declarations go into the caller's VariableEnvironment — so sloppy eval('var x = 1') inside a function creates a new local x at run time, possibly shadowing an outer x that earlier lines used. If the caller or the eval code is strict, step "If strictEval is true, set variableEnv to lexicalEnv" applies: vars stay inside the eval. EvalDeclarationInstantiation also throws a SyntaxError if a hoisted var would collide with a let or const between the eval and the function scope, and creates eval's vars as deletable bindings.
with (obj) stmt wraps the current scope in an Object Environment Record whose bindings are obj's properties, own and inherited. Each free identifier in stmt first asks HasProperty(obj, name). If the property exists and obj[Symbol.unscopables] doesn't mark the name true, reads and writes go to obj; otherwise lookup continues outward — so assigning a name obj lacks can create an implicit global. The unscopables check is why Array.prototype hides values, keys, includes and other post-ES5 methods from with blocks. Functions found on obj are called with obj as this. with is a SyntaxError in strict code, so it can't appear in modules or classes.
The result is the same in both cases: whether a name refers to a local, an outer variable, a property, or a global can depend on run-time data. Tools can't rename or remove variables safely, and engines lose static resolution.