Scrum goal being deferred.
Suppose there is a situation where the team is just 4 days away from completing the sprint. But suddenly, two very critical defects come to fix for Dev team and entire team halts their sprint goal work and starts working on the defect. This consumes all the remaining 4 days left in the sprint. Is the team still following scrum since the immediate work on defect fixing deviated the team completely from Sprint goal? What happens to spring goal then?
The Scrum Guide does talk about the cancellation of Sprints:
A Sprint could be cancelled if the Sprint Goal becomes obsolete. Only the Product Owner has the authority to cancel the Sprint.
Previous versions of the Scrum Guide have had more details about what happens when a Sprint is cancelled. However, it's typically seen as a last resort. Especially in a scaled environment with multiple teams collaborating on a single product with the Sprints as a heartbeat of coordination between the teams, cancellation may not always work.
Assuming that the Product Owner agrees that it is more valuable to fix the defects than to achieve the previously-defined Sprint Goal, then it probably makes sense for the team to shift their focus onto this new work. What happens next depends on the specifics of the situation, but I would expect that the Sprint Review and Sprint Retrospective, along with the following Sprint Planning to happen as normal. For such critical defects, I'd also expect that at least a portion of the time in the Sprint Retrospective would be dedicated to understanding how such critical defects went undetected to the point of them requiring work to stop and shift toward them.
Is the team still following scrum since the immediate work on defect fixing deviated the team completely from Sprint goal?
There's no evidence one way or the other. Are the team delivering optimum value...and are they learning from this experience, inspecting and adapting so future Sprint Goals are in fact met, and empiricism in product delivery is better achieved?