The routers require subnet mask to route the packets to other routers or hosts, but why do host machines that are not routers such as computer running Ubuntu or windows need subnet mask to be configured. It does not do any routing. Where is the subnet mask used in the non-router host machine ?
Imagine this scenario:
Host B knows it's own IP address (10.3.3.22).
Consider, if Host B needs to speak to Host A with the IP 10.3.3.11 (which we can tell exists in the same Network as Host B because of the blue circle), Host B can speak to it directly.
But How does Host B know that? It doesn't have the luxury of a pretty topology photo and blue circle to look at like we do.
Instead, it uses the Subnet Mask. Assume it has a Subnet Mask of 255.255.255.0. Host B can do a little subnetting to determine that the IP addresses 10.3.3.0 through 10.3.3.255 are on it's own network. And from there determine that 10.3.3.11 (Host A) is indeed in this range.
This is also what tells Host B that if it needs to speak to Host D, that Host D's IP address (10.4.4.44) does NOT fall in Host B's network, and therefore the packet must be sent to the Default Gateway (i.e., the Router).
More details on the process (and where the image above came from) can be found in this video: https://www.youtube.com/watch?v=QPi5Nvxaosw
Disclaimer, I created the video, hope it helps