Good question! Viola initially says she wants to serve Olivia, because Olivia has recently lost a brother:
A virtuous maid, the daughter of a count
That died some twelvemonth since: then leaving her
In the protection of his son, her brother,
Who shortly also died...
Viola, of course, sees Olivia as a kindred spirit: Viola fears at this point that her brother, Sebastian, has died. Yet the Captain then tells her Olivia
...will admit no kind of suit
No, not the Duke's.
Viola wants to keep her estate unknown, until such a time as she knows better what to do - and, perhaps, until she knows whether Sebastian has survived. So, she resolves,
I'll serve this duke
Thou shalt present me as an eunuch to him...
Why a eunuch? Well, because it explains her feminine voice. And, in Renaissance courts, eunuchs were often employed as musicians and singers: and Viola goes on to say that she can sing and play several instruments.
Viola is shipwrecked in Illyria and separated from her twin brother, Sebastian. However, from the words of the ship's captain, she has reason to hope her brother might still be alive and not drowned at sea. After questioning the captain, Viola decides first that she wants to serve Olivia, because she doesn't wish yet to reveal who she is. As a gentlewoman to Lady Olivia, she would have safety and security in a world she suspects she may now be alone in. As she puts it, she would serve that she
might not be delivered to the world,
Till I had made my own occasion mellow,
What my estate is!
When the captain tells her that Olivia, in mourning, isn't seeing anyone, the energetic Viola quickly determines to disguise herself as a male so she can become a gentleman to Duke Orsino. This serves her purpose of remaining incognito, but it's also significant that she remembers Orsino "was a bachelor." The captain confirms that he is still one; he has no wife Viola could serve in Olivia's stead. It seems her only option is to disguise herself as a man.