Your problem is definitely a grounding problem, but finding where it is may be difficult. It starts with the ground connection at the bulb and the socket. Then as was mentioned, may go through several connectors till it finally is bolted to something metal.
The brake lights and the running lights both share the same ground in the dual filament bulb. If that ground is bad, the brake lights will find a new ground, letting power go up through the running light circuit in reverse till it finds a path to ground somewhere through a component up front in the wiring harness. But when you turn the headlights on, power is now running back down the running lights circuit, butting heads with the power from the brake lights. All kinds of crazy things can happen with a poor ground.
Sometimes the quickest way to solve a grounding problem is to run a new ground wire from the ground wire at the sockets to a good place on the frame. Then make sure you have a small ground wire going from the frame up front to the neg battery post.