Alaska was tremendously far away and undeveloped, “Siberia’s Siberia.” Also at the time, the Russian Empire viewed the British Empire as the bigger competitor and threat. Selling Alaska to the Americans, Russia killed two birds with one stone — blocking British expansion from Canada into Alaska and perhaps one day Siberia, and raising money for domestic projects like the railways you mentioned.
It was actually a good sale at the time, and it was all part of the Great Game of 19th century imperialism. Russia was no babe in the woods in that game. It knew exactly what it was doing, and oh yeah, Russian treatment of the Alaska natives was also horrible as you might expect. (Not mentioned in that silly paid Kremlin article I linked, of course.)
These days, Alaska is tremendously valuable, but in those days, it was just another huge chunk of land that Russia didn’t need and likely couldn’t have held onto. If Russia had not sold Alaska, then it is certainly possible it would have been annexed by the British or the Americans or the Japanese(!) but by force.