What exactly was Spock doing in Star Trek: The Wrath of Khan that caused his death?
He was trying to correct a problem with the warp core
You will remember that Khan engaged the Enterprise in battle; this severely damaged the Enterprise, including the Warp Core. The Enterprise needed warp drive to escape the explosion of the Genesis device. Consequently (and as per Memory Alpha's description):
Spock sacrificed his own life by repairing the Enterprise's plasma conduits – in a severely irradiated portion of engineering – in order to save the crew.