Image embedded for our convenience:

Technique described here.
How is everything connected?
Choosing the serial connection is very important on the Nano. All the choices are listed here, from BEST to WORST. SoftwareSerial is the worst choice.
You should avoid the String class, as it can cause problems on these limited-RAM devices. There's no reason to build one big String for printing. Just print the pieces.
You should open the file just once, in setup. Then you can flush the file once per second, or connect a push button that closes the file and stops the sketch. Then it is safe to remove the card.
Here is your sketch, incorporating all those suggestions:
#include <NeoSWSerial.h>
#include <NeoTee.h>
#include <NMEAGPS.h>
#include <SdFat.h>
SdFat SD;
NeoSWSerial GPSserial(2,3);
NMEAGPS gps;
gps_fix fix;
File d;
Print *prints[] = { &Serial, &d };
NeoTee tee( prints, 2 );
void setup() {
Serial.begin(9600);
GPSserial.begin(9600);
SD.begin(4);
d = SD.open("datalog.txt", FILE_WRITE);
}
void loop(){
// check for characters from the GPS device and parse them
if (gps.available( GPSserial )) {
// Once per second, a complete fix structure is ready.
fix = gps.read();
if (fix.valid.location)
tee.print( fix.latitude(), 6 );
tee.print( ',' );
if (fix.valid.location)
tee.print( fix.longitude(), 6 );
tee.print( ',' );
if (fix.valid.altitude)
tee.print( fix.altitude() );
tee.println();
d.flush();
}
}
This uses my NeoGPS and NeoSWSerial libraries. They are available from the Arduino Library Manager, under the menu Sketch-> Include Library-> Manage Libraries.
To print the GPS info to 2 destinations at the same time (the Serial port and the SD file), it also uses my NeoTee library (available here). You have to manually install this library: download the zip, and extract it to a temporary location. From the temporary location, copy the nested NeoTee-master subdirectory into your Arduino/Libraries directory.
Notice that the sketch checks the validity flags before printing lat, lon or alt values. Your GPS may not know these values yet, so you shouldn't print them if the GPS device hasn't sent them.
The sketch also uses the SdFat library instead of SD. SdFat is newer and has many bug fixes and performance enhancements over the old SD library. It is available from the Library Manager.