Think you just should check the temperature and if it is too low switch heater and fan on and if it is too low switch both off. (Why those 4 seconds?)
As loop is executed multiple times per second you do not need delay's or so.
What is important is to keep the state and the previous state of the fan.
Partial code to get the idea
int heaterHIgh = 40;
int heaterLow = 35;
boolean heaterOn = false; // simple state machine reflecting the heater
boolean prevState = false;
void setup()
{
//...
}
void loop()
{
// READ SENSOR
int t = Thermister(analogRead(0));
// DO SOME STATE MACHINE MATH
if (t <= heaterLow) heaterOn = true;
else if (t >= heaterHIgh) heaterOn = false;
// ADJUST HEATER AND FAN
if ((heaterOn == true) && (prevState != true)) // state changed to true
{
prevState = true;
digitalWrite(w, HIGH);
Serial.println("AUTO HEAT ON");
digitalWrite(g, HIGH);
}
if ((heaterOn == false) && (prevState != false)) // state chenged to false
{
prevState = false;
digitalWrite(w, LOW);
Serial.println("AUTO HEAT OFF");
digitalWrite(g, LOW);
}
// other code can be here
}
Code above can be expanded so that you can set heaterhigh and heaterLow over the serial port
Furthermore it might be possible to steer the fan by means of PWM and let the RPM depend on the actual temperature.
2 cents,