There are two major ways to create a god NPC. The method depends on the purpose the character serves in the campaign. In either case, the god should help facilitate an adventure for the PCs. It is seldom a good idea to create a powerful NPC with a strong presence that fails to further the story.
The Golarion Pathifinder setting provides excellent examples of both methods with true gods and demigods, as described below.
1. Plot Device
I sometimes call this a "force of nature" NPC. A plot device god works best as some nebulous force that drives the plot by helping the PCs or complicating their lives. They rarely should directly interact with the PCs. The goal of any encounter with the plot device god should involve a quest, social encounter, or any other encounter that does not involve physical confrontation. A friendly plot device god might be a quest giver while an enemy plot device god might create a storm or a curse that can only be appeased with a quest.
Roleplaying Interactions
If the god must expose themselves to direct confrontation, then make sure all interactions are fun and interesting to the players. If a PC attacks then, don't just say "it doesn't work." Have something interesting happen, like the weapon going through them or blowing the fighter away. If you don't want this to happen at all, then have the god manifest in a way that makes it impossible for both sides to directly affect each other. Maybe the god appears as a reflection in the mirror or as a ghostly avatar.
Statistics
A god as a plot device should not have any actual statistics. Creating a stat block is unnecessary, patronizing to the players, and limits your ability as a GM what they can and cannot do. By creating a stat block, you create limitations of the character's power, which is not something you want to do when creating a character as an unassailable force of nature.
Example
True gods in the Golarion are excellent examples of plot device gods. They have no statistics and rarely directly interact with PCs. True gods affect a plot by changing the circumstances or direction of an adventure. For example, in the module Clash of the Kingslayers, a god transforms a temple into a massive walking monster that the party has to go inside and explore in order to figure out a way to stop it. In the Wrath of the Righteous adventure path, a goddess offers mythic power in exchange for passing a test.
2. Active Character
A god as an active character essentially functions as a very powerful NPC. As such, you would stat and run them as you would any sufficiently powerful NPC. Such a god directly affects the PCs with their actions. However, it's absolutely vital that the PCs can interfere with or invoke those actions. Do not have an evil god enact an evil plan unless the PCs have some way to thwart it. Do not allow a good god fix a problem unless the PCs actively do something to receive that aid.
Roleplaying Interactions
You need to determine a good reason why an active god won't wipe out a party or make an adventure trivial. An enemy god might not be able to do so or find it impractical to directly confront the PCs. Maybe they don't see the PCs as big of a threat to directly deal with? A good god might be too occupied to directly do the PCs's work for them.
Usually the best way to handle it is having the PCs only interact with NPCs that work for the god. Maybe they have to face the evil god's minions. Maybe they can only speak to a good god by their servants. For a low level campaign, an active god should work more like a plot device god.
Only have an active god directly confront the PCs if you intend to change the direction of the plot (like a plot device god) or allow the PCs to directly affect the god.
Statistics
Follow the monster creation guidelines in the Monster Manual as you would stat any other creature. Don't just make up a list of absurd stats. Instead, determine what CR the god should be based on how high of a hypothetical party should be able do and then stat them accordingly.
Examples
Demigods in the Golarion setting are good active god examples. Unlike true gods, demigods in Golarion usually have statistics as a CR 20-30 creature. They're designed as powerful final opponents for a high level party or as rare allies called upon sparingly.
The Treerazer and Arazni are good examples of enemy active gods. The Treerazer is a demon trapped in a blighted forest that slowly tries to expand his influence. Arazni is a queen of an undead nation. Both of these demigods can directly affect the party without actually confronting them. A campaign using these gods will likely see the PCs thwarting their evil plans from a distance and taking down their minions until the campaign reaches a climax that confronts the demigods themselves.
Talmandor is a good example of an allied demigod as he's a force of good that likes to take an active role in a nation's politics and crusades. However, he rarely directly interferes with mortal affairs unless directly asked or if a massive national threat occurs.
NPCs might gain experience points and levels, if the DM wants to keep track of that. There's no inherent reason they cannot do so. It depends on whether this is a one-shot for the NPC, or whether he might be a recurring character. Also it depends on the level of detail and amount of work the DM wants to do.
I try to build and run my NPCs as much like PCs as possible, and I do track their experience and levels if they are recurring people (as either allies or opponents). If they are just single-shot, disposable allies (or opponents), I don't bother. But I find it adds both realism, and good challenges for the players, if the NPCs change and grow (and learn, both from and about the party).
It can vary, even in the same party. As a player, in one long-term adventure, our party of 3 PCs had acquired quite a selection of allies, with several groups from each of several sources. Plus an NPC we had rescued. The rescued NPC we treated as a full party member, with the DM's consent, and we tracked her experience and levels. She also got death saves when knocked to 0 hp, and other such perks normally accorded to PCs. Most of the other NPCs were not so tracked, having been created as types from the MM such as "thug", "archer", etc. They remained constant and somewhat faceless, and died when knocked to 0 hp.
The DM gets to decide all of that. In my world, even the monsters get death saves and might survive if left unconscious.
How I handle any given NPC is a mix, depending on the importance of the NPC. I will often build a BBG, or a major recurring ally of the party, completely as a PC, and have him make his plans, and level him up as I go just like a PC, to keep pace with the PCs power and the needs of the story.
His main henchmen might be started with an NPC stat block from the MM or Volo's Guide, and I assign them an appropriate level and xp amount, and if they survive a while, I'll add capabilities and level them up from there, using the same progression as PCs. The NPC stat blocks have a few shortcuts and simplifications, but they aren't very different from a PC build of the same level.
Likewise for enemies that aren't the main bad guys, but may end up hanging round for a while. The party recently handled a problem with an orc tribe harassing some towns. The village had been built normally, with a War Chief, a Tanarukk, and 3 Eyes of Gruumsh, all on Giant Boars; 6 Orogs on regular boars, and about 70 Orcs, all according to the monster stat blocks. The party attacked them a few times and weakened them, but then had to go deal with something else. In this case, the party came back in a couple of weeks to finish the job, so the only changes to the orc village were some weapons and tactics, and the addition of a couple of Minotaurs that the Tanarukk had recruited (being of the same religion and all). But if the party had not come back soon, I would have had the orc village starting to train up, and the leaders level up, and them try to find some more new allies, in response to having been attacked by the party.
Lower level grunts I'll just use Thugs or whatever, and leave them at that.
For the leader types, I'll often roll them a couple magic items, as per the table on p.38 of the DMG for starting a PC at higher level. Magic items are more interesting, and better earned, if you have to face them first before you can get them.
This does get more complicated if the bad guys are not PC types. For example, my current campaign features a Hobgoblin Warlord, with a couple Devastators and a few Iron Shadows, as well as an allied rogue Mind Flayer Arcanist. For all of these, I started with the Monster stat blocks. For the Warlord, Devastators, and Mind Flayer, I am tracking experience, and leveling them up as we go to keep a couple levels ahead of the party (which is currently 8 PCs of 6th level). I may start tracking and leveling the Iron Shadows soon as well, if the party decides to pursue one of the other plot lines for a while, and gains some power and levels in the process.
Best Answer
You are looking for Contingent Spell.
It comes in both a feat (CArcane) form and a spell form.
(phb Magic chapter, 'Contingency' spell)
Revivify from the Spell Compendium is probably the ideal target. The affected doesn't lose a level from being revived. A second Contingency (only the Feat can make multiple Contingencies at once), with Heal, would bring him back to full health.
Alternatively, a Delay Spell'd (CArcane) Revivify could be your ticket. Especially if it's cast using Cloaked Casting (CMage) to disguise what spell has been cast. A psicrown containing Anticipatory Strike (CPsi) (GM can explicitly design custom psycrowns), could allow a cleric to cast a delayed revivification as an immediate action right before the last blow is struck.
Alternatively, a custom magic item
A custom wondrous item of Revivify, costing about 1,000gp, that is exhausted when used and is triggered by the wearer's death. It's a custom magic item, but the DM is explicitly allowed to make those, and 1,000gp is about right for a single-use use-activated item of a 5th level spell. Add a second effect (+150% cost of that effect) of Heal to the item, making it cost around 2500gp all up.
Make it only work for clerics of that god.