SpaceX also benefits from a constant, unbroken vision: develop cheap, reusable rockets -> lower launch costs -> build a spaceship to go to Mars. That's roughly the plan, and they've stuck to it since their founding. NASA, meanwhile, sees every new administration flip the table and change the mission, because every president wants to have their Kennedy "We choose to go to the Moon" moment". This makes long term planning impossible, and budgets are cut when Congress gets impatient.
NASA is still riding on the coattails of the Apollo program, but it takes more than that to inspire people to work for you. No doubt that NASA remains a desirable employer full of bright and motivated people eager to contribute to the larger goal, but NASA just doesn't have this "it" factor anymore. If you asked someone on the street what NASA is doing back in 1970, they would say "send people to the Moon". But today? NASA is involved in a plethora of projects, from the Mars rovers to aeronautics to climate change monitoring, which is nice, to be sure, but also sucks time and resources from the kind of "Moonshot projects" that SpaceX and other private actors are working on.