I wanted to go to Balestrand, but I eventually caved and decided to take the expedient route of staying in less-attractive Flam. I researched the Sognafjord-area transportation links about 3 weeks ago for my trip, which is one week after yours. What I found at the time was this:
The afternoon Flam-Gudvangen ferries depart at 13:45 (LB), 15:20 (F1) and 1600 (LB). Travel time is 2 hours.
The last ferry seems to be too late to catch the final scenic bus of the day. You can buy tickets on the F1 ferry from the NorwaysBest website.
As Badger reports, you'll need to take the 17:45 bus from Gudvangen to Voss, no matter which of the first two ferries you take. I believe there's more to do around Flam (although it's reportedly hyper-touristy) than around Gudvangen, so you might prefer to take the 15:20 ferry and have your layover there rather than in Gudvangen. (I've not been to either place, so what do I know?)
These are the late afternoon/evening train departures from Voss to Bergen for June 22--check for your date on www.vy.no: 16:10, 17:01, 18:08, 19:33. Travel time is just under 1-1/2 hours. Only the last departure would work for you.
Just in case something goes wrong unexpectedly: There might be an express (non-scenic) bus from Gudvangen to Voss late enough in the afternoon to be useful in a pinch. I didn't look at those schedules.
There is also a bus between Flam and Gudvangen; much of the route is through a tunnel, so it isn't scenic, but it takes only 20 minutes. Afternoon departures from Flam are at 13:00, 17:00 and 18:00. Late-afternoon departures from Gudvangen are at 17:30 and 18:30. I think tickets are normally sold by NorwaysBest in conjunction with F1 tickets for the Flam-Gudvangen (or vice versa) ferry trip; I don't know what other options exist for buying that bus ticket, or even whether it is possible to buy just the bus ticket (if one were taking the Lustrabaatane ferry rather than the F1).