The ending is actually far more hopeful than the book. Though I'm a bit confused about what happened to Miles. I'd thought he was a goner, and there was a scene in the last episode where Peter apologized to a teary Miles as if it were a done deal ...but then there was a living Miles leaving with Flora and Henry. At first I thought it must be someone else leaving in Miles' body, but that didn't happen. I guess I'll figure that one out on the second go-round.
It's a little confusing, but I think the gist is: A ghost can temporarily possess someone without permission, but the person can kick them out and/or it doesn't last long. However, with "You, Me, Us" or some other declaration of consent and a willingness to do so, then the ghost becomes a permanent inhabitant if it so desires, while the human part is stuck in the pocket memory / happy place. This was foreshadowed by the conversation back at boarding school in Episode 2, about possession and consent. However, the ghost can also leave -- as Peter did with Rebecca in Episode 6 or 7, where she gives him her body so they can be together... but he lied to her and she had trusted him. he sticks her in a "happy memory" (his misleading promise that "they can be together") but then decides that he is miserable without her, so he drowns her body in the lake... and then the fucker vacates it as she's dying because he doesn't want to go through the experience, so Rebecca jolts back to herself right as she's drowning and fully experiences her own death. Peter is such a shit.
Anyway, Miles did consent and thus was taken over by Peter as long as Peter wanted him. But then Viola merges with Dani, all the ghosts are freed and follow the natural progress. Normally spirits don't linger behind, but Viola had been the "black hole" of Bly so to speak from which none of them could naturally escape. They couldn't leave the premises as ghosts and they couldn't just naturally pass to wherever the dead normally go not at Bly. Hannah disappears. Rebecca disappears. Peter disappears. They all feel themselves going and have a short period to say goodbye but then they are gone.
Since Peter is gone now, Miles is released from the pocket memory he had been suppressed in. He's still alive. But this was the only scenario he would have lived in.
Getting back to Dani and Viola, they're merged so they can leave Bly. For whatever reason -- maybe because Viola wants to just feel alive rather than a ghost or because Dani has so much to live for / is strong in her own way -- Dani stays in control. They are merged (Dani isn't suppressed) and they both feel temporarily at peace, at being alive rather than dead. But Viola is so strong-willed and intrinsically tied to Blair that she slowly haunts Dani again, taking over.
[I forget if I mentioned it earlier, but it really evokes my haunting recollections of Heroes Season 4 I think, where Peter's brother Nathan died at the end of Season 3 but the heroes used a multiplicity of powers basically to recreate Nathan in Sylar's body (it took a few different powers). So basically Sylar forgot who he was and has become a simulacrum of Nathan. Only a few people know. Even Sylar (who looks and acts like Nathan) thinks he is Nathan...but slowly Sylar is fighting to get out and increasingly pushes back against how he was reshaped. Peter keeps wanting him to fight because he loves his brother, but he just can't and eventually Nathan turns back into Sylar. It reminds me a lot in the emotional beats of what Dani and Viola are struggling through.]
Anyway, Viola is merged with Dani but will take her over, so Dani drowns herself to keep Viola back at Bly... but now it goes both ways. Dani-Viola might have Viola's self-serving will but she also has Dani's love for people, and thus her ghost never hurts anyone again even when they go walking.
As a side note, Rebecca is much more compassionate -- despite being in a bad romance -- and sacrificing than Peter. This might make her more vulnerable to Peter's requests unfortunately. This is obvious because she doesn't go through with the plan to steal Flora's body. But note that she also had the horrible experience of DYING by drowning, with Peter's betrayal... but she is willing to put Flora in her happy place and take over her body for her, to drown a second time, to spare Flora that dreadful experience.