I understand that the elves want to Valinor when they died. However, sometimes they took boats to Valinor, such as at the end of Lord of the Rings, and other times they died of injury and had no chance to. Would they end up in the same place either way? Also, if an elf lost the will to live, would they leave in a boat, or just die like a human?
When elves die they go to Valinor and eventually their spirits are incarnated in new bodies.
It's...complicated. Fortunately, Tolkien wrote about this extensively in an essay titled "Laws and Customs Among the Eldar".
What does "death" mean for Tolkien's Elves?
It seems a little bit silly to talk about Elvish death in the first place, since the Elves are frequently described as immortal, a word which literally means "not susceptible to death." However, to Tolkien, Elves aren't really immortal; they're functionally immortal, but the natural span of their lives is not actually infinite:
"Death", therefore, is the unnatural span of an Elf's life: Tolkien defines it more precisely as the separation of feä (spirit, or soul) and hroä (body):
What happens to an Elf who dies?
As above, their spirit becomes "unhoused". That's when things get interesting:
Basically, the houseless Elvish feär are invited to return to Aman. If they do so, they get a period in purgatory and, eventually, if they desire it, reborn in Aman. In exactly one case, an Elf was re-embodied and then returned to Middle-earth; but that's an exceptional case.
However, not all feär in Mandos are reborn; some of them did not wish to return to life, and a small number (Feänor chief among them) did such bad things in life that they were not permitted rebirth).
Are Elves reborn into their old bodies, or do they get new ones?
Tolkien is unclear on this. In "Laws and Customs", he suggests that, in the vast majority of cases, Elves are reborn through childbirth, and therefore get new bodies:
He suggests that it's possible for an Elf to be reborn into their old body, but it's rare; the old body would need to be perfectly preserved and undamaged, which is an unlikely occurrence.
However, "Laws and Customs" was not Tolkien's last word on the subject; Christopher Tolkien discusses his father's changing view on the subject in an appendix on "Athrabeth Finrod ah Andreth"; in particular he reproduces part of a discussion between Manwë and Eru:
According to this version, an Elf may be reborn either in a new body, or their old body may be recreated for them; which one is at the discretion of the Valar (presumably meaning Mandos).
However, Tolkien's final word was that the Elves were reincarnated either into their original bodies (if it was available) or into exact recreations of their original bodies; Christopher Tolkien devotes a good portion of the appendix describing how Tolkien came to this conclusion, but the final word on the matter comes from a note on "Athrebeth":
What about feär that refuse the summons?
Astute readers will note something about one of my quotes above (emphasis mine):
As implied, it is entirely possible for a feä to ignore the summons of the Valar, although that reflects rather poorly on the Elf who does so. The result of that choice is also described:
These Elves essentially becomes ghosts; some are dangerous, and willing to try to "steal" bodies from the living:
What about Elves who lose the will to live?
In most cases, they simply die of their own volition, and their fate is no different to any other Elf who dies. Elves are able to will themselves to die, and this typically only occurs in Elves who "gave up hope":
In principle there's no reason why an Elf in Middle-earth who had given up hope couldn't simply take a ship over the Sea, rather than choosing to die, but I'm not aware of any examples off-hand.
The closest I can think of is Celebrían, the wife of Elrond, who left Middle-earth after being captured by Orcs:
It's not clear that she actually lost the will to live, in the strictest sense, but does indicate that Elves suffering from mental health problems (e.g. depression after being held captive by Orcs) can depart over the Sea for healing, rather than simply giving up life.
Valinor is the home of the Elves...not the afterlife. It is part of the continent of Aman, just as Middle-earth is a continent, both on our own ancient pre-history earth. At the time of LOTR many of the Elves who had been living in Middle-earth in exile had been given pardon to come home, and so were taking ship to go home. They did not sicken and die, and they aged so slowly that they didn't really age and die either. They could be killed though, or die of grief. Here from the book Letters of JRR Tolkien are brief explanations:
Ideally, Elves do not die; their lives are bound to that of the world. They are 'immortal' but not 'eternal;' their existence is "measured by the duration in time of Earth." (Tolkien, Letters 204) However, with the introduction of evil into the world, death came into the picture and marred Eru's plan for the Elves. "The Elves were not subject to disease, but they could be 'slain': that is their bodies could be destroyed, or mutilated so as to be unfit to sustain life." (Tolkien, Letters 286) They can also die of grief--essentially, give up on life. For the Elves, however, 'death' is not a true death; the fëa never leaves the world. Instead, it flees to the halls of Mandos, where it may rest and find release from the weariness of the world.
The boat trip is just that....a trip. When Galdalf (who, with the other Istari/Wizards, is also from the Undying Lands/Valinor) takes ship with Galadriel and the others, he is also merely going home after his assignment in Middle-earth was done. This assignment was given to all of the Istari, Saruman, Radagast and the 2 Blue Wizards, but Gandalf is the one who held true to the quest. The Mortals given permission to enter Valinor are still mortal, and if they stay there they'll receive healing and forget mental anguish, but they will still die there.