How to kill a character in a way that feels earned and not just, "Oh, they're gone," before moving on.
Have it affect the plot in a way that is beneficial to the story, regardless if you've been foreshadowing the character's death (but their role usually is a big enough of a clue, so like big brother/father-figure characters to the protagonist tend to get flagged for death). It can be a net positive or negative for the characters (typically, you want protagonists to be able to grow from it to prove themselves worthy to be independent from the deceased), but it has to be taking the story in the direction you want without killing its soul as deaths (when done
properly) signify turning points in a story.
For TV dramas, the death of a character can also be a way for the audience to differentiate between "arcs" or seasons, or even the
feel of the show for the foreseeable future. It's risky since depending on the character, you may lose the audience and therefore ratings, but that's why you need to have good characters to take up that yoke to make up for their loss and keep the audience invested. In comedies, this is a lot trickier since the writers are walking that extremely fine line to prove their worth
as writers as well as testing the audience's loyalty, in a way, since there
are people who don't like having their emotions manipulated.
The Simpsons did this quite a bit for better or for worse even though some of those decisions weren't always planned due to real life circumstances; I'm sure everyone here can attest to how much the death of Maude Flanders (despite being a polarizing character) affected the show as well as left an impact on us. (Mufasa's death would've been a much better example, but I was talking about TV shows, not movies, so *shrug*.)
Literature is much more flexible when it comes to character deaths since there's no real-life actors to write scripts around, but if it wasn't originally planned, creator breakdowns can lead to the author choosing to kill off a character(s) which is just as much of a risky move. But if it's a series, again, there's more wriggle room and allows the author to work out the kinks before the
wham! moment happens.
Deaths in fan fiction should have the same wriggle room, but you'll still have the same struggles in keeping the reader invested if they're not happy with the death of a specific character, and
especially if the genre doesn't "call for it". Which is a silly thought, but you may have seen this happen if you've been around the fan fic community enough, and why it is they have a specific fic genre for it: death fic. Fans would rather have it be its own separate thing instead of having it show up suddenly in a story, but if you're mainly writing for yourself and you still have plans for a character to die, you should still be careful how you tread even
if it's appropriate for the narrative. Definitely study how media over the course of its existence has treated death and why it is there are certain character deaths that affect viewers and even pop culture better than the other fictional deaths.
There's a reason why all creators are always reminded to never be afraid to kill their darlings. It's an umbrella term that can mean exactly what it says (killing off a character), but also it means to not be afraid to scrap everything to start over should you find yourself stuck. Anything from an art project to an entire storyline is a creator's "darling" that is always at risk of being killed, and they may not ever come back. Even if you're playing favorites with your cast, you must absolutely be ready to have to kill your favorite(s) to save your project from hitting a brick wall. Fan fic writers, despite figuratively playing around with their favorite toys, are no exception, but it's extremely easy to forget that.