When you use a foundation, it's not supposed to be affected by the terrain at all, regardless of whether it's level or not. The only time the terrain does affect the foundation is when the "constrainfloorelevation" cheat is set to false.
There might be an easy way to fix it, though, depending on how extensive the problem is.
Go into build mode and enter the cheat: constrainfloorelevation false. Then use the Level Terrain tool inside the house on all of the affected parts of the foundation and the second floor (if necessary). This should affect the walls as well. If it doesn't, enter "constrainfloorelevation true" and remove the parts of the walls that don't look right and rebuild them.