A simple use case would be:
batch_size = 10
in_features = 20
out_features = 1

lin = nn.Linear(
    in_features=in_features,
    out_features=out_features
)

x = torch.randn(batch_size, in_features)
out = lin(x)
print(out.shape)  # [batch_size, out_features]

The input is specified as [batch_size, in_features], so in your first example, you would use a batch of 5 samples, each containing 20 features.
The second example is a bit more complicated.
dim1 in this case refers to “additional” dimensions, which can be seen as applying the linear layer in a loop for each batch:

x = torch.randn(batch_size, 5, in_features)
out = lin(x)
out1 = out.clone()

out2 = []
for idx in range(5):
    out = lin(x[:, idx])
    out2.append(out.clone())
out2 = torch.stack(out2, dim=1)

# Compare both outputs
print(torch.allclose(out1, out2))
> True