NextJS and Gatsby both use the ReactJS framework for front-end functionality but the fundamental difference between these two tools is that NextJS requires a server to run and Gatsby can be functioning without a server environment.
The second main difference is the handling of data that is adopted by these two tools, Gatsby is a little instructive i.e it tells you how you should manage your data while Next takes instructions and provides a free hand to users.
Gatsby generates the static HTML pages on build time while Next.JS does the same dynamically at request time.