I have this problem and know exactly how you need to fix it. Go to your wifi settings, then hit the menu soft button on the lower left (to the left of the single push button on the front), then click Advanced. Uncheck the "Auto network switch" check box. For some reason this automatically disconnects from the wifi quickly before you have a chance to enter the p/w and show the phone that it has connectivity to the internet. This often re-checks itself randomly.... VERY annoying.
Overall the Wifi on the Samsung Galaxy S4 is a piece of crap (I have problems with some routers as well), but everything else work great.