How do I optimize the window lengths in STFT?
This is the classic "uncertainty principle" of the Fourier Transform. You can either have high resolution in time or high resolution in frequency but not both at the same time. The window lengths allow you to trade off between the two.

If you want to detect "events" in your EEG signal with a resolution of say 10ms, then this should be your window length. This will give you a frequency resolution of about 100 Hz.