Visiting Flaam from Bergen

My husband and I are visiting Norway very soon. We want to travel to Flam on the 14th September from Bergen and return to Bergen from Flam on the 16th September.
I am a little confused as to where the famous flam railway goes to and from.
I had thought we might catch a fast ferry from Bergen to Flam and return on the train – or visa versa.
Could you possibly help out with suggestions of times costs and approximate travel times involved.
Thank you very much.
Kind regards
Jane D.